C++- Please solve TODO sections and match sample output. Demonstrate event-contr
ID: 3904355 • Letter: C
Question
C++- Please solve TODO sections and match sample output.
Demonstrate event-controlled loop using boolean flag to
present a menu. Use nested decision structure to process
each choice including invalid response.
*/
#include<iostream>
#include<iomanip>
using namespace std;
const char QUIT = '0'; // constants for menu options
const char INPUT = '1';
const char PROCESS = '2';
const char OUTPUT = '3';
int main()
{
char userSelection; // input for menu choice
bool done; // flag for loop control
// TODO: Initialize the loop control variable (done).
// Think about what an appropriate value for 'done' would be.
// Are we done at the beginning of the program?
// TODO: Begin loop here. Test the loop control variable
// Do NOT test the userSelection variable
// display the menu
cout << endl
<< setw(25) << " Program Menu " << endl
<< setw(25) << "---------------------" << endl
<< setw(25) << "1 ...... Input " << endl
<< setw(25) << "2 ...... Process " << endl
<< setw(25) << "3 ...... Output " << endl
<< setw(25) << "0 ...... Quit Program" << endl
<< setw(25) << "---------------------" << endl;
// get user response
cout << setw(20) << "Enter option: ";
cin >> userSelection;
// TODO: Process menu responses using If-Then-Else-If.
// For each option, print appropriate message as shown
// in sample output below, including invalid choice.
// Also, be sure to use the provided constants when
// you are testing values.
// TODO: End loop here.
cout << " End Program - ";
}
/* Sample Program Interaction and Output
Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 1
Do INPUT stuff
Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 2
Do PROCESS stuff
Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 3
Do OUTPUT stuff
Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 4
Invalid choice, please try again.
Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: q
Invalid choice, please try again.
Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 0
End Program - Press any key to continue . . .
*/
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
const char QUIT = '0'; // constants for menu options
const char INPUT = '1';
const char PROCESS = '2';
const char OUTPUT = '3';
int main()
{
char userSelection; // input for menu choice
bool done; // flag for loop control
// TODO: Initialize the loop control variable (done).
// Think about what an appropriate value for 'done' would be.
// Are we done at the beginning of the program?
done = false;
// TODO: Begin loop here. Test the loop control variable
// Do NOT test the userSelection variable
// display the menu
while(!done) {
cout << endl
<< setw(25) << " Program Menu " << endl
<< setw(25) << "---------------------" << endl
<< setw(25) << "1 ...... Input " << endl
<< setw(25) << "2 ...... Process " << endl
<< setw(25) << "3 ...... Output " << endl
<< setw(25) << "0 ...... Quit Program" << endl
<< setw(25) << "---------------------" << endl;
// get user response
cout << setw(20) << "Enter option: ";
cin >> userSelection;
// TODO: Process menu responses using If-Then-Else-If.
// For each option, print appropriate message as shown
// in sample output below, including invalid choice.
// Also, be sure to use the provided constants when
// you are testing values.
// TODO: End loop here.
if(userSelection == '1') {
cout << "Do INPUT stuff" << endl;
} else if(userSelection == '2') {
cout << "Do PROCESS stuff" << endl;
} else if(userSelection == '3') {
cout << "Do OUTPUT stuff" << endl;
} else if(userSelection == '0') {
done = true;
} else {
cout << "Invalid choice, please try again." << endl;
}
}
cout << " End Program - ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.