Tech Blog » Home » Coders Forum » Computer Directory » Math Calculators » RSS:Directory|Forum
C++ Source Code: Implementations, Beginners
C++ » Source Code » C++ Links » for Loops » File Streams » Switch » Coders Community: Forum
// - Menu with switch control structure.
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "MENU\n\n";
cout << "1." << "\t" << "Lobster\n";
cout << "2." << "\t" << "Steak\n";
cout << "3." << "\t" << "Turkey\n";
cout << "4." << "\t" << "Hamberger\n";
cout << "5." << "\t" << "Vegetarian\n\n";
cout << "Choose your dinner entree: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1: cout << "Lobster is my favorite! Dig in!\n\n";
break;
case 2: cout << "Yummy! Steak is great...\n"
<< "but limit yourself to once a week," << endl
<< "or risk the chance of high cholesterol!\n\n";
break;
case 3: cout << "Turkey is healthier than steak! ...Enjoy!\n\n";
break;
case 4: cout << "Hamburger is another form of steak. :-)\n\n";
break;
case 5: cout << "Finally, a vegitarian is in the house!\n\n";
break;
default: cout << "Invalid number, please enter a number"
<< " from the entrees above.\n\n";
break;
}
return 0;
}
/*============================[output]==================================
MENU
1. Lobster
2. Steak
3. Turkey
4. Hamberger
5. Vegetarian
Choose your dinner entree: 2
Yummy! Steak is great...
but limit yourself to once a week,
or risk the chance of high cholesterol!
Press any key to continue . . .
====================================[output 2]=========================
MENU
1. Lobster
2. Steak
3. Turkey
4. Hamberger
5. Vegetarian
Choose your dinner entree: 6
Invalid number, please enter a number from the entrees above.
Press any key to continue . . .
======================================================================*/
// - Switch using characters.
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "A)\tHouse MD\n"
<< "B)\tAmerican Idle\n"
<< "C)\tFamily Guy\n\n";
cout << "What TV show do you like (Enter A, B, C)?: ";
cin >> letter;
cout << endl;
switch (toupper(letter))
{
case 'A' : cout << "Dr. House is a radical doctor!\n\n";
break;
case 'B' : cout << "Wannabe singers!\n\n";
break;
case 'C' : cout << "One of the craziest cartoons Ive ever seen.\n\n";
break;
default: cout << "Invalid choice.\n\n";
break;
}
return 0;
}
/*============================[output]=================================
A) House MD
B) American Idle
C) Family Guy
What TV show do you like (Enter A, B, C)?: a
Dr. House is a radical doctor!
Press any key to continue . . .
=============================[output 2]================================
A) House MD
B) American Idle
C) Family Guy
What TV show do you like (Enter A, B, C)?: z
Invalid choice.
Press any key to continue . . .
======================================================================*/
// - Menu using if, else if, else statements.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "MENU\n\n";
cout << "1." << "\t" << "Lobster\n";
cout << "2." << "\t" << "Steak\n";
cout << "3." << "\t" << "Turkey\n";
cout << "4." << "\t" << "Hamberger\n";
cout << "5." << "\t" << "Vegetarian\n\n";
cout << "Choose your dinner entree: ";
cin >> num;
cout << endl;
if (num == 1)
{
cout << "Lobster is my favorite! Dig in!\n\n";
}
else if (num == 2)
{
cout << "Yummy! Steak is great...\n"
<< "but limit yourself to once a week,\n"
<< "or risk the chance of high cholesterol!\n\n";
}
else if (num == 3)
{
cout << "Turkey is healthier than steak! ...Enjoy!\n\n";
}
else if (num == 4)
{
cout << "Hamburger is another form of steak. :-)\n\n";
}
else if (num == 5)
{
cout << "Finally, a vegitarian is in the house!\n\n";
}
else
{
cout << "Invalid number, please enter a number"
<< " from the entrees above.\n\n";
}
return 0;
}
/*=====================[output]=======================================
MENU
1. Lobster
2. Steak
3. Turkey
4. Hamberger
5. Vegetarian
Choose your dinner entree: 5
Finally, a vegitarian is in the house!
Press any key to continue . . .
============================[output 2]================================
MENU
1. Lobster
2. Steak
3. Turkey
4. Hamberger
5. Vegetarian
Choose your dinner entree: 0
Invalid number, please enter a number from the entrees above.
Press any key to continue . . .
======================================================================*/

Forum: