Need help on following program using C++ language, without using global variable
ID: 3687146 • Letter: N
Question
Need help on following program using C++ language, without using global variable.
Jason opened a coffee shop at the beach and sells coffee in three sizes: small (9oz), medium (12oz), and large (15oz). The cost of one small cup is $1.75, one medium cup is $1.90, and one large cup is $2.00. Write a menu-driven program that will make the coffee shop operational. Your program should allow the user to do the following:
Buy coffee in any size and in any number of cups.
At any time show the total number of cups of each size sold.
At any time show the total amount of coffee sold.
At any time show the total money made.
The buy coffee option allows the customer to select the number of cups in each of the sizes they want to order and display a receipt for that order. The other three options would be used by the owner to check on the status of the shop during the day. Pay attention to the user interface. Try to minimize the number of mouse clicks and display output neatly.
Write source code which should be commented as follows:
Analysis at the top of the file
Function analysis, preconditions, and postconditions after each function prototype
Major tasks identified as comments in main function
Comments in functions as needed
Explanation / Answer
hey heres the code:
#include<iostream>
using namespace std;
struct coffeecups /*i used struct to record number of cups each customer buys*/
{
int cup1;
int cup2;
int cup3;
}a[100];
void buy(int );
void dispcof(int);
void dispmon(int);
void dispcups(int);
void receipt(int);
int main()
{
int l=0,n=0,op;
cout<<"Welcome to coffee shop";
while(n!=1) /*this loop repeats till user gives input 5 (i.e. bye) */
{
cout<<" 1. Buy coffee 2. Display total number of cups sold 3.Display total amount of coffee sold 4. Total amount of money made 5. Bye ";
cout<<" Choose one option: ";
cin>>op;
switch(op){
case 1:
buy(l);
l++;
break;
case 2:
dispcups(l);
break;
case 3:
dispcof(l);
break;
case 4:
dispmon(l);
break;
case 5:
n=1;
break;
}
}
return 0;
}
void buy(int l) //This function is used to take customers orders to buy coffee
{
int op,c1=0,c2=0,c3=0,n;
cout<<" How many cups of coffee you want?";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<" 1.Small cup 2.Medium cup 3.Large cup";
cout<<" choose an option for cup "<<i+1<<" : ";
cin>>op;
if(op==1)
{
c1++;
a[l].cup1=c1;
a[l].cup2=c2;
a[l].cup3=c3;
}
if(op==2)
{
c2++;
a[l].cup1=c1;
a[l].cup2=c2;
a[l].cup3=c3;
}
if(op==3)
{
c3++;
a[l].cup1=c1;
a[l].cup2=c2;
a[l].cup3=c3;
}
}
receipt(l);
}
void dispcof(int n)
{
/*this function displays quantity of coffee sold.*/
int i,q=0;
for(i=0;i<n;i++){
if(a[i].cup1>0){
q=q+a[i].cup1*9;
}
if(a[i].cup2>0){
q=q+a[i].cup2*12;
}
if(a[i].cup3>0){
q=q+a[i].cup3*15;
}
}
cout<<" Total quantity of coffee sold : "<<q;
}
void dispmon(int n){ /*This function is used to display total money gained*/
int i;
float tmon=0;
for(i=0;i<n;i++){
if(a[i].cup1>0){
tmon=tmon+a[i].cup1*1.75;
}
if(a[i].cup2>0){
tmon=tmon+a[i].cup1*1.90;
}
if(a[i].cup3>0){
tmon=tmon+a[i].cup1*2.00;
}
}
cout<<" Total amount of money : "<<tmon;
}
void dispcups(int n){ /*This function displays total number of cups sold*/
int tot=0,c1=0,c2=0,c3=0,i;
for(i=0;i<n;i++)
{
c1=c1+a[i].cup1;
c2=c2+a[i].cup2;
c3=c3+a[i].cup3;
tot=tot+a[i].cup1+a[i].cup2+a[i].cup3;
}
cout<<" you have sold "<<tot<<" cups.";
cout<<" Small cups: "<<c1;
cout<<" Medium cups: "<<c2;
cout<<" Large cups: "<<c3;
}
void receipt(int n){ /*(it shows receipt) This function is called immediately after buy() function executes(i.e. after customer buys coffee*/
int c1,c2,c3;
float tot=0.0;
cout<<" Thankyou for coming, heres your bill: ";
if(a[n].cup1>0){
c1=a[n].cup1;
tot=tot+c1*1.75;
cout<<" "<<c1<<"x 1.75 - Small cup";
}
if(a[n].cup2>0){
c2=a[n].cup2;
tot=tot+c2*1.90;
cout<<" "<<c2<<"x 1.90 - Medium cup";
}
if(a[n].cup1>0){
c3=a[n].cup3;
tot=tot+c3*2.00;
cout<<" "<<c3<<"x 2.00 - Large cup";
}
cout<<" Total amount: "<<tot;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.