What is wrong w/ following C++ code? Using XCode #include<iostream> #include<cty
ID: 3726980 • Letter: W
Question
What is wrong w/ following C++ code? Using XCode
#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"
}
}
}
Explanation / Answer
use break; in default case also
#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";break; // 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";break; // 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"; break; // display "Bad Input"
}
}
}
Output:
==== 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:
a
Did A
==== 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:
A
Did A
==== 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: Exit Menu
E
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.