int main() { int Ag; string NameA; string NameB; cout << "Hello!\nWelcome To Josh's World!\n"; system("PAUSE"); cout << "First, though, I'll need some Info:\n"; system("PAUSE"); cout << "Your First Name:"<<endl; cin >> NameA; cout << "Your last Name:"<<endl; cin >> NameB; system("PAUSE"); cout << "And I must confirm that you are over 13 years old.\nYour Age?"; cin >> Ag; int age(); cout <<"Hello!"; system("PAUSE"); return 0; } int age() { int Ag; if (Ag < 13); { cout << "You are TOO young!"; system("PAUSE"); return 0; } else { cout << "Welcome To Josh's World!"; system("PAUSE"); return main(); } }
Any help is appreciated.
From reading the code, I can see that as it stands "age" is meant to be a function of the Main program. When declaring a function you should always place the function above the main function. As when compiling it will read from the top and come down, however if you do not have the function before the main, you will get a lot of errors. At sometimes because of this it gives you a lot of things as errors which really aren't, like for example that parse error after the else.
Taking into account that the age does not really return any kind of value, it should be made of data type void as opposed to int. This is where I believe more errors occurred.
Please read these notes on Functions to more understand what I have been talking about.
Here is the corrected code:
void age(void) { int Ag; cout << "And I must confirm that you are over 13 years old.\nYour Age?"; cin >> Ag; if (Ag <= 12) { cout << "You are TOO young!"; system("PAUSE"); return ; } else { cout << "Welcome To Josh's World!"; system("PAUSE"); return ; } }
int main() { int Ag, ans; string NameA; string NameB; cout << "Hello!\nWelcome To Josh's World!\n"; // system("PAUSE"); cout << "First, though, I'll need some Info:\n"; // system("PAUSE"); cout << "Your First Name:"<<endl; cin >> NameA; cout << "Your last Name:"<<endl; cin >> NameB; // system("PAUSE"); age(); cout <<"Hello!"; system("PAUSE"); return 0; }
Note: I commented out some of those system pauses, they were getting annoying to see while running the program.
EDIT: I almost forgot, I also moved the prompt and read age lines to the function Age, because while they were in the main function, when control was handed over to the age function the number stored in Ag was not carried over to the age function, there for the if statement was not being used properly. In that no matter what the age was, they would still get the "Welcome to Josh's World" message as opposed to the other one.
Quote: from Bzoink at 5:29 pm on Nov. 6, 2007 I thought ifs didn't have semicolons after them... Oh damn, I didn't notice that, that's definitely it, get rid of the semicolon after If (condition)
I thought ifs didn't have semicolons after them...
Oh damn, I didn't notice that, that's definitely it, get rid of the semicolon after If (condition)