I need help on this problem!! I have the answers but I need to know HOW to revise the code. I'm still a newbie trying to learn.
Question: Step 2: Compile the program and then run it 5 times. For each run enter the menu choice shown in the table below and write down the current output the program displays.
Run Menu choice Current output New output
1 1 You picked red. You picked red.
2 2 You picked blue. You picked blue.
3 3 You picked yellow. You picked yellow.
4 0 You picked yellow. You must choose 1, 2, or 3.
5 99 You picked yellow. You must choose 1, 2, or 3.
Step 3: Improve the program so that only 1, 2, and 3 are accepted as valid choices. Make line 24 check for a choice of 3 before printing the message on line 25. Then add a trailing else that prints a descriptive error message whenever anything other than 1, 2, or 3 is entered.
Step 4: Recompile the program and run it 5 more times, using the same 5 menu choices shown above. For each run, write down the new output the program now displays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
1 // PA 4 color.cpp
2 // This program lets the user select a primary color from a menu.
3 // PUT YOUR NAME HERE.
4 #include <iostream>
5 #include <string>
6 using namespace std;
7
8 int main()
9 {
10 int choice; // Menu choice should be 1, 2, or 3
11
12 // Display the menu of choices
13 cout << "Choose a primary color by entering its number. \n\n";
14 cout << "1 Red \n" << "2 Blue \n" << "3 Yellow \n";
15
16 // Get the user's choice
17 cin >> choice;
18
19 // Tell the user what he or she picked
20 if (choice == 1)
21 cout << "\nYou picked red.\n";
22 else if (choice == 2)
23 cout << "\nYou picked blue.\n";
24 else
25 cout << "\nYou picked yellow.\n";
26
27 return 0;
28 }
|