Develop a flowchart and then write a menu-driven C++ program that uses selection
ID: 3680701 • Letter: D
Question
Develop a flowchart and then write a menu-driven C++ program that uses selection and repetition constructs as needed, and several user-defined functions to solve the following problem.
Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu.
The menu options are shown below.
Help AddIntegers MulDoubles Quit
Once the menu item is selected and validated, the program will proceeds with processing that selection as explained below.
If the user selects h or H, a user-defined function help() will be invoked. The function displays a help screen for the user. The help should tell the user how the program operates and what is needed from the user and how to terminate the program.
Once the user viewed the help screen, striking any key will clear the screen followed by display of the menu again in function main().
If the user selects a, or A, the user is prompted for two numbers of type integer. This option will then pass the two numbers to the function addIntegers() and the function computes the sum and returns the sum to the function main() where the operands and the sum will be displayed. If the two integer numbers are 12 and 20, then the result is displayed as shown below.
The sum of 12 + 20 = 32
Once the user viewed the result, striking any key will clear the screen followed by display of the menu again in function main().
If the user selects m or M, the program invokes the function MulDoubles(). Inside function MulDoubles(), the user is prompted for two numbers of type double. The function computes the product and returns the result to the function main() where the operands and the product are displayed. If the two double numbers are 12.45 and 20.5, then the result is displayed as shown below.
The product of 12.450 * 20.500 = 255.225
Use appropriate formatting flags to show three digits after the decimal point.
Once the user viewed the result, striking any key will clear the screen followed by display of the menu again in function main().
If the user selects q or Q, the program invokes the function quit() where the following message is displayed and the program is then terminated. The program will be terminated in function main().
The program terminated per the user request…
Any other menu item must be flagged as an error and the user is warned to make another selection from the menu, and the menu is displayed again. The wrong choice should not terminate the program.
Once the program begins execution, it will not terminate until the user inputs q or Q.
For data of type integer, check your program with the following sets of data:
20, -30
-50, -82
172, -225
For data of type double, check your program with following sets of data:
3.5, 9.8
52.45, -38.82
-475.46, -184.65
Explanation / Answer
/**C++ program that displays a menu of choices and prompts
user to enter user choice. Then prompt for appropriate values
and print the results to console. If user enters q to quit the program.*/
//menu.cpp
//header files
#include<iostream>
#include<conio.h>
using namespace std;
//function prototypes
char menu();
void help();
int addIntegers(int,int);
double MulDoubles(double,double);
void quit();
//start of program
int main()
{
bool repeat=true;
char ch;
while(repeat)
{
//calling menu method
ch=menu();
switch(ch)
{
case 'h':
case 'H':
//call help method
help();
break;
case 'a':
case 'A':
int intValue1;
int intValue2;
cout<<"Enter value1 : ";
cin>>intValue1;
cout<<"Enter value2 : ";
cin>>intValue2;
//call addIntegers method
cout<<"The sum of"<<intValue1<<"+"<<intValue2<<"= "<<
addIntegers(intValue1,intValue2)<<endl;
break;
case 'm':
case 'M':
double doubValue1;
double doubValue2;
cout<<"Enter value1 : ";
cin>>doubValue1;
cout<<"Enter value2 : ";
cin>>doubValue2;
//call MulDoubles method
cout<<"The product of"<<doubValue1<<"+"<<doubValue2<<"= "<<
MulDoubles(doubValue1,doubValue2);
break;
case 'q':
case 'Q':
//call quit method
quit();
break;
}
}
system("pause");
return 0;
}
//The method quit close the program
void quit()
{
cout<<"Closing the program."<<endl;
getch();
exit(0);
}
//The method MulDoubles takes two double values and
//returns the sum of two double values
double MulDoubles(double value1,double value2)
{
return (value1+value2);
}
//The method addIntegers takes two integer values and
//returns the sum of two integer values
int addIntegers(int value1, int value2)
{
return (value1+value2);
}
//The method menu that displays a list of choices
//and prompt user to enter a choice
char menu()
{
char choice;
cout<<" ======================"<<endl;
cout<<" Help"<<endl;
cout<<" AddIntegers"<<endl;
cout<<" MulDoubles"<<endl;
cout<<" Quit"<<endl;
cout<<" ======================"<<endl;
cin>>choice;
return choice;
}
//Method help will display how to use program
void help()
{
cout<<"For Help, type h or H"<<endl;
cout<<"For AddIntegers, type a or A"<<endl;
cout<<"For MulDoubles, type m or M"<<endl;
cout<<"For Quit, type q or Q"<<endl;
}
------------------------------------------------------------------------------------------------------------
Sample output:
======================
Help
AddIntegers
MulDoubles
Quit
======================
h
For Help, type h or H
For AddIntegers, type a or A
For MulDoubles, type m or M
For Quit, type q or Q
======================
Help
AddIntegers
MulDoubles
Quit
======================
a
Enter value1 : 20
Enter value2 : -30
The sum of20+-30= -10
======================
Help
AddIntegers
MulDoubles
Quit
======================
a
Enter value1 : -50
Enter value2 : -82
The sum of-50+-82= -132
======================
Help
AddIntegers
MulDoubles
Quit
======================
a
Enter value1 : 172
Enter value2 : -225
The sum of172+-225= -53
======================
Help
AddIntegers
MulDoubles
Quit
======================
m
Enter value1 : 3.5
Enter value2 : 9.8
The product of3.5+9.8= 13.3
======================
Help
AddIntegers
MulDoubles
Quit
======================
m
Enter value1 : 52.45
Enter value2 : -38.82
The product of52.45+-38.82= 13.63
======================
Help
AddIntegers
MulDoubles
Quit
======================
m
Enter value1 : -475.46
Enter value2 : -184.65
The product of-475.46+-184.65= -660.11
======================
Help
AddIntegers
MulDoubles
Quit
======================
q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.