In: Computer Science
Write the program with language C++ ( THE PROGRAM SHOULD CONTAIN DECISION MAKING , LOOPING AND A FUNCTION ) to design a menu for a shop . When the user makes a choice , ask for quantity, then display the total charge with sale taxes . If the user enters a choice that is unavailable ,display an error message and ask him to re-enter.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
#include <iostream>
using namespace std;
// function to display menu.
void menu()
{
cout<<"\n1. Show items\n";
cout<<"2. Buy an item\n";
cout<<"3. checkout\n";
cout<<"Please enter your choice: ";
}
// function to display all the items.
void show_items()
{
// Let's say only 2 items are available
cout<<"Available items in the shop
are:\n";
cout<<"\t1. Pen ( 5 Rupees each
)\n";
cout<<"\t2. Pencil ( 3 Rupees each
)\n";
}
int main()
{
// declare variables
double total=0;
// LOOP HERE
while(1)
{
// print menu
menu();
// read the choice
int ch;
cin>>ch;
// DECISION MAKING
USING IF ELSE
if(ch==1)
{
show_items();
}
else if(ch==2)
{
show_items();
cout<<"\nPlease enter the item number: ";
int
item;
cin>>item;
if(item==1)
{
cout<<"Enter the Quantity of pens: ";
int quantity;
cin>>quantity;
cout<<quantity<<" Pens Successfully added to your
cart."<<endl;
cout<<"The cost of "<<quantity<<" pens is:
"<<quantity*5;
// add it to total
total += quantity*5;
}
else
if(item==2)
{
cout<<"Enter the Quantity of Pencils: ";
int quantity;
cin>>quantity;
cout<<quantity<<" Pencils Successfully added to your
cart."<<endl;
cout<<"The cost of "<<quantity<<" pencils is:
"<<quantity*3;
// add it to total
total += quantity*5;
}
else
{
cout<<"Invalid Choice";
}
}
else if(ch==3)
{
cout<<"\n\nThank You for shopping. Your total bill
is:\n";
cout<<"Total Price: "<<total<<endl;
//
add 10% tax
cout<<"Sales Tax: "<<total*0.1<<endl;
cout<<"FINAL BILL: "<<total + total*0.1
<<endl;
break;
}
else
{
cout<<"Invalid Choice. Please Try again..!!";
}
}
return 0;
}
========


OUTPUT


