rewrite switch structure to if...else structure

Hi. im doing assignment. there is a question ask us to rewrite a switch structure to if...else structure.

Question:

#include<iostream>
using namespace std;
int main()
{
int num;

cout<<"Enter an integer between 0 and 10: ";
cin>>num;

cout<<"\nThe number you entered is "<<num<<endl;

switch (num);
{
case 0:
case 1:
cout<<"Hello ";
case 2:
cout<<"there ";
case 3:
cout<<"I am ";
case 4:
cout<<"Mickey."<<endl;
break;
case 5:
cout<<"How ";
case 6:
case 7:
case 8:

cout<<"are you?"<<endl;
break;
case 9:
break;

case 10:
cout<< "Have a nice day."<<endl;
break;
default:
cout<<"Sorry, the number is out of" << "range."<<endl;
}

cout<<"Out of the switch structure."<<endl;

system ("pause");
return 0;
}



It's alright if i rewrite in this way?
any suggestion to improve?
thx


#include<iostream>
using namespace std;
int main()
{
int num;

cout<<"Enter an integer between 0 and 10: ";
cin>>num;

cout<<"\nThe number you entered is "<<num<<endl;

if (num == 9);
else if (num == 10)
cout<<"Have a nice day."<<endl;
else if (num <=1)
cout<<"Hello there. I am Mickey."<<endl;
else if (num == 2)
cout<<"there. I am Mickey."<<endl;
else if (num == 3)
cout<<"I am Mickey."<<endl;
else if (num == 4)
cout<<"Mickey."<<endl;
else if (num == 5)
cout<<"How are you?"<<endl;
else if (num == 6, 7, 8)
cout<<"are you?."<<endl;
else
cout<<"Sorry, the number is out of range."<<endl;

cout<<"Out of the switch structure."<<endl;

system ("pause");
return 0;
}

No. You cannot do else if (num == 6, 7, 8)

It's
else if ( (num == 6) || (num == 7) // etc

I'd also make you last else into else if (num != 9) and remove the if (num == 9) from the top.
if i remove the if (num ==9) what do i need to put at the top there?
Thx
Nothing. The else from the following line is also removed.
would you please show paste it here to have more clearer view?
thx
=\ I've given you pretty clear instructions on how to modify your code. I don't think I need to copy and paste your code. You should have no problems making those changes.
oh ok. Thx. Sorry, because i'm very new to C++. Thanks again.
Topic archived. No new replies allowed.