ch3-Menu (break, continue, switch statements)
Chapter_3 Exercise_3-2 | Exercise_3-3 |
CONTENTS: Menu1.cpp Menu2.cpp
Menu1.cpp TCP1, p. 133-134 download
// Simple menu program demonstrating the use of "break" and "continue"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char c; // To hold the response (input from keyboard)
while(true) // for ever
{
cout << "MAIN MENU:" << endl;
cout << "l: left, r: right, q: quit -> ";
cin >> c;
if(c == 'q')
{break;} // Out of "while(1)"
// else
if(c == 'l')
{
cout << "LEFT MENU:" << endl;
cout << "Select a or b: ";
cin >> c;
if(c == 'a')
{
cout << "You chose 'a'" << endl;
continue; // Back to main menu, to the next iteration of while()
}
if(c == 'b')
{
cout << "You chose 'b'" << endl;
continue; // Back to main menu
}
else
{
cout << "You didn't choose a or b!" << endl;
continue; // Back to main menu
}
}
// else
if(c == 'r')
{
cout << "RIGHT MENU:" << endl;
cout << "Select c or d: ";
cin >> c;
if(c == 'c')
{
cout << "You chose 'c'" << endl;
continue; // Back to main menu
}
if(c == 'd')
{
cout << "You chose 'd'" << endl;
continue; // Back to main menu
}
else
{
cout << "You didn't choose c or d!" << endl;
continue; // Back to main menu
}
}
// else
cout << "You must type l or r or q!" << endl;
} // end of while()
cout << "Quitting menu..." << endl;
return 0;
} // end of main()
/*
g++ Menu1.cpp -o Menu1
./Menu1
MAIN MENU:
l: left, r: right, q: quit -> n
You must type l or r or q!
MAIN MENU:
l: left, r: right, q: quit -> l
LEFT MENU:
Select a or b: a
You chose 'a'
MAIN MENU:
l: left, r: right, q: quit -> l
LEFT MENU:
Select a or b: c
You didn't choose a or b!
MAIN MENU:
l: left, r: right, q: quit -> r
RIGHT MENU:
Select c or d: d
You chose 'd'
MAIN MENU:
l: left, r: right, q: quit -> r
RIGHT MENU:
Select c or d: q
You didn't choose c or d!
MAIN MENU:
l: left, r: right, q: quit -> q
Quitting menu...
*/
Menu2.cpp TCP1, p. 135-136 download
// A menu using a switch statement
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char c; // To hold the response (input from keyboard)
bool quit = false; // Flag for quitting
while(quit == false)
{
cout << "Select a, b, c, or q to quit: ";
char response;
cin >> response;
switch(response)
{
case 'a':
cout << "You chose 'a'" << endl;
break;
case 'b':
cout << "You chose 'b'" << endl;
break;
case 'c':
cout << "You chose 'c'" << endl;
break;
case 'q':
cout << "Quitting menu..." << endl;
quit = true; // to exit while
break; // out of switch()
default:
cout << "Please use a, b, c, or q!" << endl;
break;
} // end of switch()
} // end of while()
return 0;
} // end of main()
/*
g++ Menu2.cpp -o Menu2
./Menu2
Select a, b, c, or q to quit: a
You chose 'a'
Select a, b, c, or q to quit: b
You chose 'b'
Select a, b, c, or q to quit: c
You chose 'c'
Select a, b, c, or q to quit: d
Please use a, b, c, or q!
Select a, b, c, or q to quit: q
Quitting menu...
*/
Chapter_3 Exercise_3-2 | BACK_TO_TOP | Exercise_3-3 |
Comments
Post a Comment