Problem with if else statement

Hello im new to the forums and figured I would sign up to see if anyone could help me with this problem I am having. I just started programming and am doing self exercises so I remember what I read and how it looks actually being done in a program, but doing this, i have come up with a syntax error. I am trying to use bolean statements with if/else if and else statements.

The problem I am getting is that during my else statement which is

else(age>21) {

cout "" You are not allowed in";

}

I keep getting an error that is tellingme that it is expecting a ; before the {

I don't know if this is allowed but for further help to you guys ill write my code out and tell you on what line the error is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main ()

int age,money;

cout <<"How old are you\n"
cin >> age;

cout <<"How much money do you have\n";
cin >>money;

if (age>21 && money>600) {

cout << "You are allowed in\n";

else if (age>21||money>600) {

cout << "Stand by you may be allowed in";

}

else(age<21) {

cout << "Sorry you are not allowed in"

On the last else statment I keep getting the error:expected ';' before '{' token thing. I can fix it easily by putting the ; in, but I don't understand why it needs it. Also I woudl try putting a not statement in, but I still am a bit confused on how they work. Thanks in advance
Last edited on
You cannot have a condition with the else. Make it an else if. else is designed to catch any other condition.

Please always use code tags - select your code then press the <> button on the right.

There are other problems, but will help once there are code tags.

Any compilation errors - post the full compiler output.
Ok sorry for the latency, I have put up the code with the instructions you have given me, I am using codeblocks for my compiler and the only specific error it has is..

Line 26
error:expected ';' before '{' token

Im using Codeblocks, if there is anything i can do for you to help me, with other errors this program may have let me know.

Also thanks for the quick responce
I fixed the bit of code you put up. Your main error was that you put a condition after an else statement, so either use an else statement without a condition or just use an else if statement and put your conditions after it

ex. of else statement:
1
2
else 
{cout<<"Sorry you are not allowed in";}


ex. of else if statement:
1
2
else if (age<21)
{cout<<"Sorry you are not allowed in";}


See corrected code for more instructions, and other mistakes.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int main ()
{
int age,money;

cout <<"How old are you\n";  // <---Missing a semicolon here
cin >> age;

cout <<"How much money do you have\n";
cin >>money;

if (age>21 && money>600) {

cout << "You are allowed in\n";
     } // <---You didn't end your if statement.

else if (age>21||money>600) {

cout << "Stand by you may be allowed in";

}

else {     // <--- For an else statement you cant have a condition
              //	 You can only have a condition with an else if statement.

cout << "Sorry you are not allowed in";

} // <---You didn't end your else statement
return 0;
}
Just got my internet back and would just like to say I got the problems running with the help of both of you, will be using this site for more help in the future, great great people thanks :)!
Topic archived. No new replies allowed.