C++ Help! Write a program that displays a menu, Keeps track of the number of tim
ID: 3796668 • Letter: C
Question
C++ Help! Write a program that displays a menu, Keeps track of the number of times the menu displays(executed). If the user selects any option, other than exit increment the count and redisplay the menu. If the user selects exit option, increment the count, terminate the loop and display the number of times the loop executed. When the user makes a valid selection, show them what their menu selection was. If the user selects invalid selection show the message “Invalid input, please try again! ” and redisplay the loop. (Use a char variable to store the user’s input. Use a do-while loop to display the menu until a valid selection is made. Use a switch statement to control what gets outputted based on the user’s input.) Sample Output 1 (user input in italics) Program's menu. Please select an action 1 - Option 1 2 - Option 2 3 - Option 3 e – Exit >5 Invalid input, please try again! Program's menu. Please select an action 1 - Option 1 2 - Option 2 3 - Option 3 e - Exit >1 You selected Option 1. Program's menu. Please select an action 1 - Option 1 2 - Option 2 3 - Option 3 e - Exit >e Exiting loop…. Loop iterated 3 times. Here's what I've got started: #include <iostream> using namespace std; int main() { //cout << "Hello world!" << endl; char c; int count =0; do{ //Write switch case in here }while(c!='e'); cout << "Loop execued "<< count << " times " << endl; return 0; }
Explanation / Answer
ANSWER:
#include <iostream>
using namespace std;
int main()
{
char c;
int count = 0;
do {
cout << "Program's menu. Please select an action" << endl;
cout << "1 - Option 1" << endl << "2 - Option 2" << endl;
cout << "3 - Option 3" << endl << "e – Exit; " << endl;
cin >> c;
++count;
switch(c) {
case '1' : cout << "You selected Option 1." << endl;
break;
case '2' : cout << "You selected Option 2." << endl;
break;
case '3' : cout << "You selected Option 3." << endl;
break;
case 'e' : cout << "Exiting loop… ";
break;
default : cout << "Invalid input, please try again!" << endl;
}
}while(c!='e');
cout << "Loop execued "<< count << " times " << endl;
return 0;
}
Code is working fine. Let me know any concerns. Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.