help by 7:20

i have class at 7:30 and cant figure out why this isnt working for the life of me, ive been at it for hours today and hours yesterday.

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;
}
[3595 byte] By [Sylvarius123] at [2007-11-20 11:18:36]
# 1 Re: help by 7:20
checkAge(deadAge);

in this call, you are giving the variable deadAge to the function, but you have not given this variable any value. you should pass the function with the age that you pull from the file, age.

I dont think you are understanding how functions and parameters work. The parameters that you pass to the function are copied into the variable in the defenition of the funciton

void func(int a)
{
cout << a;
}

int main()
{
int a=20;
int b=5;
func(b);
}

This will display 5, since 'b' is passed to the function. Also note that the 'a' in the function is of a local scope, so it is no the same as the a from the main function. Try something like this:

bool func(int a)
{
if(a>120)
return 0;
return 1;
}

int main()
{
int age=140;

if(func(age))
{cout << I am alive!;}
else
{cout << I am Dead!;}

}
bovinedragon at 2007-11-10 22:26:00 >
# 2 Re: help by 7:20
thanks, its weird, i learned functions and understood them, and i did them correctly for about two weeks, then its like i completely forgot how to do them.
Sylvarius123 at 2007-11-10 22:27:00 >