#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string menuChoices[] = {"Waffles","Pancakes","Eggs & Toast","Cereal"}; //Choices for the menu
cout<<"What would you like for breakfast?"<<endl;
for (int i=0;i<4;i++)
{
cout<<i+1<<"\t\t"<<menuChoices[i]<<endl; //Prints every choice in the menu like:
//1 Waffles
//2 Pancakes
//etc...
}
int myChoice;
cout<<"(Please enter a number): ";
cin>>myChoice; //get the choice
switch (myChoice)
{
case 1:
cout<<"Ahh waffles are our signature dish. They'll be right out."<<endl;
break;
case 2:
cout<<"Oh you'd like Pancakes? Good choice. We are famous for our delicious buttermilk pancakes."<<endl;
break;
case 3:
cout<<"Eggs & Toast? A tad casual but everybody loves a breakfast they could have easily made at home!"<<endl;
break;
case 4:
cout<<"Cereal? Why in the world would you come to a restaurant and have cereal... You're crazy!"<<endl;
break;
}
return 0;
}
So there you go..
Though this is supposed just supposed to be an example of application of the switch statement, not a tutorial. Like firedraco said, you can read that in the tutorial section.
Also, don't often expect source code like this to just kindof appear.. I'm just hungry and haven't reached my programming quota for the day :D
#include <sstream>
//...
int myChoice;
while (1)
{
for (int i=0;i<4;i++)
{
cout<<i+1<<"\t\t"<<menuChoices[i]<<endl; //Prints every choice in the menu like:
//1 Waffles
//2 Pancakes
//etc...
}
string input="";
cout<<"(Please enter a number): ";
getline(cin,input);
stringstream ssinput(input);
if (ssinput >> myChoice)
{
if (myChoice<=4 && myChoice>0)
break;
}
cout<<endl;
}
//...
Wasn't a hint at you. It was a hint for the OP to do some reading and not ask out-right for people to do their homework/assignment without actually making any effort themselves.
oh.. well that article is kindof irrelevant to the topic's main question..
I mean it doesn't even cover the switch statement... so I was surprised u would link to it in this topic unless u were addressing my poor use of cin>> :P
The switch statement has been explained by yourself. I pre-emptively answered the next question of how to limit input because using >> doesn't work properly blah blah. Usual BS.