in C++ 1. Write a function call displayMenu without parameters which will displa
ID: 3813764 • Letter: I
Question
in C++
1. Write a function call displayMenu without parameters which will display a menu
2. write a function called getChoice that has no parameters. It will ask the user’s choice and validate it
(only accept 1,2,3,4,and 5). The function will return the valid choice.
3. write a function called showTotal which will use the number of bouquets and the unit price passed to
it as arguments to compute and show the total price of the purchase.
4 Write the main function as follows:
a) define the unit prices of different types as constants.
d) call function displayMenu to display the menu.
c) call getChoice function to get a valid choice
d ) if the choice got in c) is not 5, ask the user how many bouquets they want to buy, use switch
statements to determine the unit price and show the total price by call function showTotal.
e) If choice is not 4, do a) to d) again.
ITEM UNIT PRICE
1 $ 5.25
2 $ 3.10
3 $ 9.75
4 $ 6.50
5 quit
Explanation / Answer
#include<iostream>
using namespace std;
void displayMenu()
{
cout<<" ITEM UNIT PRICE";
cout<<" 1. $5.25";
cout<<" 2. $3.10";
cout<<" 3. $9.75";
cout<<" 4. $6.50";
cout<<" 5. Quit";
}
int getChoice()
{
int ch;
cout<<" Enter your choice: ";
cin>>ch;
if(ch>=1 && ch<=5)
return ch;
else
{
cout<<"Invalid choice!!";
return getChoice();
}
}
void showTotal(float price,int num)
{
float totalPurchase;
totalPurchase=price*num;
cout<<" Total purchase: "<<totalPurchase<<endl<<endl;
}
int main()
{
const float price1=5.25;
const float price2=3.10;
const float price3=9.75;
const float price4=6.50;
int choice,noOfBouquets;
do
{
displayMenu();
choice=getChoice();
if(choice!=5)
{
cout<<" How many bouquets you want to order? ";
cin>>noOfBouquets;
}
else
{
cout<<" Quitting...";
exit(0);
}
switch(choice)
{
case 1:
showTotal(price1,noOfBouquets);
break;
case 2:
showTotal(price2,noOfBouquets);
break;
case 3:
showTotal(price3,noOfBouquets);
break;
case 4:
showTotal(price4,noOfBouquets);
break;
default:
cout<<"Invalid choice!!";
}
}while(choice!=4);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.