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. :)
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.
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 elseif (age >=100) and it worked great.
#include <iostream>
usingnamespace 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;
}
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.
}