In this lesson, you learned more about Visual C++'s operators and about its switch capabilities. You saw the following:
Project 5 Listing. Using a switch statement to control a menu.
1:// Filename: PROJECT5.CPP 2:// Uses a switch statement to control a user's selection 3:#include <iostream.h> 4: 5:void main() 6:{ 7: int menu; 8: float charge = 0.00; // Holds total charge 9: 10: cout << "** Computer Repair Center **" << endl << endl; 11: cout << "What work was performed? Here are the choices: " 12: << endl << endl; 13: cout << "\t1. Replaced the CPU and RAM" << endl; 14: cout << "\t2. Replaced the RAM only" << endl; 15: cout << "\t3. Repaired the monitor" << endl; 16: cout << "\t4. Fixed stuck keys" << endl; 17: 18: do 19: { 20: cout << endl << "What work was performed? "; 21: cin >> menu; 22: } while ((menu < 1) || (menu > 4)); 23: 24: // Store the charge based on the repair person's input 25: switch (menu) 26: { 27: case 1 : charge = 200.00F; // Notice no break here 28: case 2 : charge += 150.00F; 29: break; 30: case 3 : charge = 75.00F; 31: break; 32: case 4 : charge = 12.00F; 33: break; 34: } 35: 36: // Print the results 37: cout.precision(2); 38: cout.setf(ios::showpoint); 39: cout.setf(ios::fixed); 40: cout << endl << "The total charge is $" << charge << endl; 41:}
Output
** Computer Repair Center ** What work was performed? Here are the choices: 1. Replaced the CPU and RAM 2. Replaced the RAM only 3. Repaired the monitor 4. Fixed stuck keys What work was performed? 5 What work was performed? 1 The total charge is $350.00
Description
1: A C++ comment that includes the program's filename.
2: A C++ comment that contains the program's description.
3: cout and cin need information in the IOSTREAM.H header file.
4: Extra blank lines make your program more readable.
5: All functions have names, and the first function in all C++ programs is main().
6: All functions begin with a left brace.
7: Defines an integer variable that will hold the user's chosen menu option.
8: Defines a floating-point variable that will hold a charge.
9: Extra blank lines make your program more readable.
10: Prints a title.
11: Prepares to print the menu.
12: Continues the previous line, printing two new lines.
13: The first menu choice.
14: The second menu choice.
15: The third menu choice.
16: The fourth menu choice.
17: Blank lines help to make your program more readable.
18: The do-while loop always executes at least once, ensuring that the body with the user's question will be shown.
19: while loops use braces to contain the statements that are to be repeated.
20: Asks the user for the desired menu option.
21: Gets the user's input.
22: Keeps looping if the user doesn't enter a valid menu option.
23: Extra blank lines make your program more readable.
24: Place comments throughout your code.
25: Start of the switch statement that will select a case block based on the user's response to the menu.
26: The switch statement encloses its choices in braces.
27: The charge for a CPU repair is stored.
28: The charge for RAM replacement is stored.
29: The break ensures that the execution won't fall through to the subsequent case blocks.
30: The charge for monitor repair is stored.
31: The break ensures that the execution won't fall through to the subsequent case blocks.
32: The charge for the keyboard repair is stored.
33: The break ensures that the execution won't fall through to the subsequent case blocks.
34: A final closing switch brace is always required.
35: Extra blank lines make your program more readable.
36: Place comments throughout your code.
37: Ensures that two decimal places print.
38: Makes sure the decimal point always appears.
39: Guards against scientific notation.
40: Prints the total charge.
41: A final brace ends all main() functions.
22: Always check the user's input for validation.
27: Notice that there is no break here. If there is a CPU repair, there will always be a RAM replacement (see the menu), so execution will fall through to the second case as well.
28: Removing a break lets you cascade case code blocks.
29: If execution fell through from the previous case, charge will hold $350. Otherwise, if the user selected only RAM replacement, the charge will be only $150.00.