The document contains 5 examples of C++ code using switch statements to evaluate conditions and output corresponding responses. In each example, the code prompts the user for input, evaluates the input using a switch statement, and prints a response based on the input case. The examples demonstrate using switch statements with characters, integers, and a default case to handle invalid inputs.
1. Switch example 1
http://eglobiotraining.com.
#include <iostream>
using namespace std;
int main(void)
{
char grade;
cout << "Enter your grade: ";
cin >> grade;
switch (grade)
{
case 'a':case 'A':
cout << "Your average must be between 90 - 100"
<< endl;
break;
case 'b':
case 'B':
cout << "Your average must be between 80 - 89"
<< endl;
break;
case 'c':
case 'C':
cout << "Your average must be between 70 - 79"
<< endl;
break;
case 'd':
case 'D':
cout << "Your average must be between 60 - 69"
<< endl;
break;
default:
cout << "Your average must be below 60" << endl;
}
return 0;
}
2. Screenshot
http://eglobiotraining.com
Enter your grade: 1
Your average must be below 60
3. Switch example 2
• #include <iostream> http://eglobiotraining.com
• using namespace std;
• int main()
• {
• int choice;
• cout << "MENUnn";
• cout << "1." << "t" << "Lobstern";
• cout << "2." << "t" << "Steakn";
• cout << "3." << "t" << "Turkeyn";
• cout << "4." << "t" << "Hambergern";
• cout << "5." << "t" << "Vegetariannn";
• cout << "Choose your dinner entree: ";
• cin >> choice;
• cout << endl;
• switch (choice)
• {
• case 1: cout << "Lobster is my favorite! Dig in!nn";
• break;
• case 2: cout << "Yummy! Steak is great...n"
• << "but limit yourself to once a week," << endl
• << "or risk the chance of high cholesterol!nn";
• break;
• case 3: cout << "Turkey is healthier than steak! ...Enjoy!nn";
• break;
• case 4: cout << "Hamburger is another form of steak. :-)nn";
• break;
• case 5: cout << "Finally, a vegitarian is in the house!nn";
• break;
• default: cout << "Invalid number, please enter a number"
• << " from the entrees above.nn";
• break;
• }
• return 0;
• }
4. Screenshot
http://eglobiotraining.com
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!
5. Switch example 3
http://eglobiotraining.com
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "A)tHouse MDn"
<< "B)tAmerican Idlen"
<< "C)tFamily Guynn";
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!nn";
break;
case 'B' : cout << "Wannabe singers!nn";
break;
case 'C' : cout << "One of the craziest cartoons Ive ever seen.nn";
break;
default: cout << "Invalid choice.nn";
break;
}
return 0;
}
6. Screenshot
http://eglobiotraining.com
/
*============================[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 . . .
============================================
==========================*/
7. Switch example 4
http://eglobiotraining.com
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "MENUnn";
cout << "1." << "t" << "Lobstern";
cout << "2." << "t" << "Steakn";
cout << "3." << "t" << "Turkeyn";
cout << "4." << "t" << "Hambergern";
cout << "5." << "t" << "Vegetariannn";
cout << "Choose your dinner entree: ";
cin >> num;
cout << endl;
if (num == 1)
{
cout << "Lobster is my favorite! Dig in!nn";
}
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!nn";
}
else if (num == 3)
{
cout << "Turkey is healthier than steak! ...Enjoy!nn";
}
else if (num == 4)
{
cout << "Hamburger is another form of steak. :-)nn";
}
else if (num == 5)
{
cout << "Finally, a vegitarian is in the house!nn";
}
else
{
cout << "Invalid number, please enter a number"
<< " from the entrees above.nn";
}
8. Screenshot
http://eglobiotraining.com
/
*=====================[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 . . .
=============================================
=========================*/
9. Switch Example 5
http://eglobiotraining.com
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int n;
printf("Please enter a number: ");
scanf("%d", &n);
switch (n) {
case 1: {
printf("n is equal to 1!n");
break;
}
case 2: {
printf("n is equal to 2!n");
break;
}
case 3: {
printf("n is equal to 3!n");
break;
}
default: {
printf("n isn't equal to 1, 2, or 3.n");
break;
}
}
system("PAUSE");
return 0;
}