help by 7:20
i have to read in this information from the file names.txt located in the same folder.
23 Justin Schonfeld Timberlake
67 Sushil Louis
12 Michael Jackson Leverington
29 Chang Jia
3 Leandro Robinho Loss
8 Bilal Gonen
23 Juan Q
234 Jesse James
21 Mark
then take that and find out who is dead, and who can vote and cannot vote. also i have to find who is the oldest and youngest.
please no major changes to the program, im sure its a bad program since this is only the fourth one ive written.
the problem seems to be in passing the variable age to the functions, because when i cout age in the functions it gives me garbage.
#include<iostream>
#include<fstream>
#include<cmath>
#include<cstring>
//prototypes
int checkAge(int age);
int checkVote(int age);
using namespace std;
int main(){
ifstream fin; //variables
string name;
string currentOldest;
string currentYoungest;
int age;
int voteAge;
int deadAge;
int currentHAge = 0;
int currentLAge = 10000000;
fin.open("names.txt"); //opens names.txt
if (!fin.fail()){ //checks for file
while (!fin.eof()) { //runs the reader until end of file
fin >> age; //takes ages
getline(fin,name); //takes names
checkAge(deadAge); //runs checkAge function to determine if the person is alive or dead
checkVote(voteAge); //runs checkVote function to determine if the person can vote
//if the person is alive then this runs
if(deadAge == 1){
if(age > currentHAge){ //this finds the oldest person
currentHAge = age;
currentOldest = name;
}
if(age < currentLAge){ //finds the youngest person
currentLAge = age;
currentYoungest = name;
}
if(voteAge == 1){ //checks to see if the person is old enough to vote
cout << "Old enough to vote: " << name << endl;
}else{
cout << "Not old enought to vote: " << name << endl;
}
}
}
//outputs oldest and youngest person
cout << currentOldest << " is the oldest person." << endl;
cout << currentYoungest << " is the youngest person." << endl;
fin.close();
}
system("PAUSE");
return 1;
}
//finds who is alive
int checkAge(int age){
cout << age << endl;
int deadAge;
if(age >= 120){
deadAge = 0;
}else{
deadAge = 1;
}
return deadAge;
}
//finds who can vote
int checkVote(int age){
int voteAge;
if(age >= 18){
voteAge = 1;
}else{
voteAge = 0;
}
return voteAge;
}

