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;
=\ 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.