C++ use the following code and follow instructions (tasks) to create he expected
ID: 3726823 • Letter: C
Question
C++ use the following code and follow instructions (tasks) to create he expected output:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char menuT;
do {
cout << "==== Menu Top ==== ";
cout << "A GoTo Menu A ";
cout << "B GoTo Menu B ";
cout << "E Exit: ";
cin >> menuT;
while (1) {
switch (menuT) {
case 'A':
cout << "==== Menu A ==== ";
cout << "A Do Action A ";
cout << "B Do Action B ";
cout << "E Exit: ";
cin >> menuT;
break;
case 'B':
cout << "==== Menu B ==== ";
cout << "A << Do Action A ";
cout << "B << Do Action B ";
cout << "E Exit: ";
cin >> menuT;
break;
case 'E':
return 0;
default:
cout << "Bad input" << endl;
}
if (menuT != 'A' || menuT != 'B') {
break;
}
}
} while (1);
}
Explanation / Answer
PROGRAM
#include<iostream>
#include<ctype.h>
using namespace std;
void menuA(); // Prototype Declare menuA() function
void menuB(); // Prototype Declare menuB() function
void callMenu()
{
char ch; // Declare character variable ch
while(1) // create inifinity while loop until choice "E"
{
// Display Main Menu
cout<<" ==== Menu Top ===== ";
cout<<"A GoTo Menu A"<<endl;
cout<<"B GoTo Menu B"<<endl;
cout<<"E Exit: ";
cin>>ch; // read choice
switch(ch) // Declare multiconditional statement
{
case 'A':
case 'a': menuA(); break; // check condition 'A' or 'a' then call menuA() function
case 'B':
case 'b': menuB(); break; // check condition 'B' or 'b' then call menuB() function
case 'E':
case 'e': cout<<"Exit Menu";exit(0); // Display "Exit Menu" and terminate entire program
default: cout<<"Bad Input"; // display "Bad Input"
}
}
}
int main()
{
callMenu(); // call callMenu() function
return 0;
}
void menuA() // implement menuA() function
{
char ch; // Declare character variable ch
while(1) // create infinity while loop until choice "E"
{
// Display Menu A
cout<<" ==== Menu A ==== ";
cout<<" A Do Action A"<<endl;
cout<<" B Do Action B"<<endl;
cout<<" E Exit: ";
cin>>ch; // Read Option character
switch(ch) // Declare multiconditional statement
{
case 'A':
case 'a': cout<<" Did A"; break; // check condition 'A' or 'a' then display 'Did A'
case 'B':
case 'b': cout<<" Did B"; break; // check condition 'B' or 'b' then display 'Did B'
case 'E':
case 'e': cout<<" Exit Menu";callMenu(); break; // Display "Exit Menu" and goto callMenu()
default: cout<<" Bad Input"; // display "Bad Input"
}
}
}
void menuB() // implement menuB()
{
char ch; // Declare Character variable ch
while(1) // create inifinity while loop until choice "E"
{
// Display Menu B
cout<<" ==== Menu B ===== ";
cout<<" A Do Action A"<<endl;
cout<<" B Do Action B"<<endl;
cout<<" E Exit: ";
cin>>ch; // Read choice
switch(ch) // Declare multiconditional statement
{
case 'A':
case 'a': cout<<" Did A"; break; // check condition 'A' or 'a' then display 'Did A'
case 'B':
case 'b': cout<<" Did B"; break; // check condition 'B' or 'b' then display 'Did B'
case 'E':
case 'e': cout<<" Exit Menu";callMenu(); break; // Display "Exit Menu" and goto callMenu()
default: cout<<" Bad Input"; // display "Bad Input"
}
}
}
OUTPUT:
==== Menu Top =====
A GoTo Menu A
B GoTo Menu B
E Exit: 2
Bad Input
==== Menu Top =====
A GoTo Menu A
B GoTo Menu B
E Exit: a
==== Menu A ====
A Do Action A
B Do Action B
E Exit: 2
Bad Input
==== Menu A ====
A Do Action A
B Do Action B
E Exit: e
Exit Menu
==== Menu Top =====
A GoTo Menu A
B GoTo Menu B
E Exit: b
==== Menu B =====
A Do Action A
B Do Action B
E Exit: 2
Bad Input
==== Menu B =====
A Do Action A
B Do Action B
E Exit: a
Did A
==== Menu B =====
A Do Action A
B Do Action B
E Exit: b
Did B
==== Menu B =====
A Do Action A
B Do Action B
E Exit: e
Exit Menu
==== Menu Top =====
A GoTo Menu A
B GoTo Menu B
E Exit: e
Exit Menu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.