PDA

View Full Version : [C++] Newbie, something's wrong with my code



dodle
March 11th, 2009, 02:06 AM
#include <iostream>
#include <string>
using namespace std;


int main()
{
int age;

cout << "How old are you?";
cin >> age;

if (age < 100)
cout << "You are younger than 100";
else if (age == 100)
cout << "You are 100";
else (age > 100)
cout << "You are older than 100";

return 0;
}Output during compiling:
g.cpp: In function ‘int main()’:
g.cpp:19: error: expected `;' before ‘cout’
There is something that I am not seeing. According to the tutorial I was reading, my code looks fine to me.

issih
March 11th, 2009, 02:10 AM
I suspect your problem lies in having a condition after the else statement.

else is a catch all, no condition goes with it, try removing the "(age > 100)" and compiling again.

Hope that helps

P.S. in essence I think it is interpreting (age > 100) as the statement to run in the else block, but you supply no closing ; so the compiler gets confused

dodle
March 11th, 2009, 02:11 AM
Lol, sorry, what a silly mistake. Good thing I put "Newbie" in the title. Thanks.

jsmidt
March 11th, 2009, 02:13 AM
Change it to this:



#include <iostream>
#include <string>
using namespace std;


int main()
{
int age;

cout << "How old are you?";
cin >> age;

if (age < 100)
cout << "You are younger than 100";
else if (age == 100)
cout << "You are 100";
else
cout << "You are older than 100";

return 0;
}


Don't give else and 'if' clause.