Else without a previous if

Sep 27, 2011 at 4:38pm
I'm just starting out at C++ and I keep getting the error: 'else' without a previous 'if' even though I'm sure it should work.
Here's my code:

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
#include <iostream>

using namespace std;

int main()
{
   int age;
   cout <<"Please enter your age: ";
   cin>> age;
   cin.ignore();
   cin.get();
   if (age<20);
   {cout<<"Hey, you're pretty young.";

   else if (age<40);
   {
       cout<<"Still got a long way to go.";
   }
   else if (age<80);
   {
       cout<<"You're getting old.";
   }
   else (age>=100);
   {
       cout<<"Start counting seconds.";
   }
   }

}
Last edited on Sep 27, 2011 at 4:42pm
Sep 27, 2011 at 4:41pm
Please use the code tags when posting code.

In either case: the 'if' statement doesn't need a semicolon (';').

Also: the 'else' statement doesn't need a condition. "age >=100" isn't being evaluated. Simply putting "else" will include all ">=80" cases right now.
Last edited on Sep 27, 2011 at 4:42pm
Sep 27, 2011 at 4:42pm
Your ifs and else-ifs should not have semicolons after them. Also, your first if is missing a closing } and you have one closing } too many at the end. Finally, an else clause shouldn't have a condition, so get rid of the ( ) and everything in between. :)

EDIT: Infernal ninjas.

-Albatross

Last edited on Sep 27, 2011 at 4:42pm
Sep 27, 2011 at 4:45pm
Ok, it works now, but when I enter a number less than 20, it also prints the statement for over or equal to 100. Or any other number aswell actually if its not 100.
Last edited on Sep 27, 2011 at 4:47pm
Sep 27, 2011 at 4:46pm
What does your updated code look like?

-Albatross
Sep 27, 2011 at 4:48pm
Here it 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
28
#include <iostream>

using namespace std;

int main()
{
   int age;
   cout <<"Please enter your age: ";
   cin>> age;
   cin.ignore();
   if (age<20)
   {cout<<"Hey, you're pretty young.";
   }
   else if (age<40)
   {
       cout<<"Still got a long way to go.";
   }
   else if (age<80)
   {
       cout<<"You're getting old.";
   }
   else (age>=100);
   {
       cout<<"Start counting seconds.";
   }


}


EDIT: Also tried without the ( ), but it's the same.
Last edited on Sep 27, 2011 at 4:50pm
Sep 27, 2011 at 4:55pm
1
2
3
4
else (age>=100);
   {
       cout<<"Start counting seconds.";
   }


Leave the part in bold out, then it works.
Last edited on Sep 27, 2011 at 4:55pm
Sep 27, 2011 at 4:56pm
You forgot to remove the condition and ; on line 22.

EDIT: My ninja skills are dropping.

-Albatross
Last edited on Sep 27, 2011 at 4:56pm
Sep 27, 2011 at 4:56pm
Nope, it just says what it should for whatever number, but with the 'Start counting seconds.' as well.
Sep 27, 2011 at 4:57pm
It works fine for me if you make the correction hanst99 indicated.

-Albatross
Sep 27, 2011 at 4:58pm
I notice you still have a semi-colon after else (age>=100) Remove it and things should work better. Also, you're not checking if age is between 80 and 99.

EDIT: I tried it with else if (age >=100) and it worked great.
Last edited on Sep 27, 2011 at 5:00pm
Sep 27, 2011 at 5:01pm
Okay thanks, it works now that I've added to test between age 80-99.
Sep 28, 2011 at 10:58am
wouldnt a switch (case) be easier than multiple if else? just a suggestion.

eg.
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
#include <iostream>

using namespace std;

int main()
{
   int age;
   cout <<"Please enter your age: ";
   cin>> age;
   cin.ignore();
   swich (age)   {
case (age< 20);
     cout<<"Hey, you're pretty young.";
break;
case (age<40);
         cout<<"Still got a long way to go.";
break;
case (age<80);
          cout<<"You're getting old.";
break;
default;
        cout<<"Start counting seconds.";
break;

}
 




Sep 28, 2011 at 12:08pm
Switch doesn't work with conditionals. You could, however, transform the age value:

 
int iage = age/20;

This will turn <20 into 0, 20->40 into 1 and so on. Then, switch(iage) with case 1,2,3,4 & default will cover the entire age spectrum.
Sep 28, 2011 at 12:20pm
sorry my bad. set up the switch wrong, this would work, correct me if im wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch(age)
{
case 1:
age < 20; //condition 1
cout << "Hey, you're pretty young" << endl;
break;
case 2:
age < 40; //condition 2
cout << "Still got a long way to go" << endl;
break;
case 3:
age<80; // condition 3
cout << "You're getting old" << endl;
break;
default: //default condition
cout << "Start counting seconds" << endl;
}
Sep 28, 2011 at 12:49pm
No, again: switch doesn't take conditionals. In fact, nothing takes conditionals in the form you just posted. In your code, lines 4, 8, and 12 are the same as typing "0;" or "1;" in your code. Even if it does compile, it will simply ignore those lines.

If you really want to use a switch, do this:
1
2
3
4
5
6
7
8
int iage = age/20;
switch(iage) {
  case 0: // This is the "age < 20" case. 
 ...
  case 3: // This it the "age < 80" case. 
  default: // This is the "age >= 80" case.
}
Sep 28, 2011 at 1:13pm
@Gaminic, this code compiled fine, but yea, like u said it ignores them lines, i thought switches could accept conditions, my bad.
Topic archived. No new replies allowed.