#include <iostream>
using namespace std;
int main()
switch(age)
{
case 1:
case 2:
case 3:
case 4:
cout<< "Tricycle"<<endl;
break;
case 5:
.
.
case 15:
if (15 >= age >= 5)
cout<< "Bicycle"<<endl;
break;
case 16:
case 17:
cout<<"Motorcycle"<<endl;
break;
default:
cout<< "Motor car"<<endl;
return 0;
}
Error message:
Expected initializer before switch
I tried declaring age as an integer...but still I get an error message.
There are several mistakes in your code:
You are missing an opening
{aftermain().You did not declare
age, or give it a value, before trying to use it.Simply not value code. So I assume you did this only for demonstration purposes here, and in your real code you are actually spelling out each
caseindividually.This simply does not do what you think it does. C++ comparisons do not work this way. You need separate expressions to compare
ageto each value individually.Not to mention, if you have individual
casestatements for5..15, then thisifis completely redundant anyway.You are missing a closing
}on theswitchblock beforereturn.With that said, try the following instead:
Alternatively, using
ifinstead ofswitch: