#include <iostream>
#include <string>
usingnamespace std;
int multiply (int a,int b)
{
int r;
r=a*b;
return(r);
}
int subtract (int a,int b)
{
int r;
r=a-b;
return (r);
}
void beverages()
{
cout<<"Beverages"<<endl<<endl;
cout<<"Coke - 12"<<endl;
cout<<"Sprit - 12"<<endl;
cout<<"Coffee - 5"<<endl;
}
void snacks()
{
cout<<"Snacks"<<endl;
cout<<"Sandwich - 15"<<endl<<endl;
cout<<"Hamburger - 15"<<endl;
cout<<"Spagetti - 15"<<endl;
}
int main()
{
int drinks[2];
drinks[0]=12;
drinks[1]=12;
drinks[2]=5;
int snack[2];
snack[0]=15;
snack[1]=15;
snack[2]=15;
int choice;
int choice_2;
int cost;
int quantity;
int total=0;
int cash;
int change;
char more;
again:
snacks();
cout<<endl;
beverages();
cout<<endl;
cout<<"Choice:"<<endl;
cout<<"1. Snacks"<<endl; cout<<"2. Beverages"<<endl<<endl;
cout<<"Please place your order:";
cin>>choice;
while (true)
{
if (choice==1)
{
system("cls");
snacks();
cout<<endl;
cout<<"Select your choice:";
cin>>choice_2;
if (choice_2==1)
{
cout<<"Quantity:";
cin>>quantity;
cout<<endl;
cout<<"Cost:";
cost=multiply(snack[0],quantity);
total=total+cost;
cout<<cost<<endl;
break;
}elseif (choice_2==2)
{
cout<<"Quantity:";
cin>>quantity;
cout<<endl;
cout<<"Cost:";
cost=multiply(snack[1],quantity);
total=total+cost;
cout<<cost<<endl;
break;
}elseif (choice_2==3)
{
cout<<"Quantity:";
cin>>quantity;
cout<<endl;
cout<<"Cost:";
cost=multiply(snack[2],quantity);
total=total+cost;
cout<<cost<<endl;
break;
}
}
elseif (choice==2)
{
system("cls");
beverages();
cout<<endl;
cout<<"Select your choice:";
cin>>choice_2;
if (choice_2==1)
{
cout<<"Quantity:";
cin>>quantity;
cout<<endl;
cost=multiply(drinks[0],quantity);
total=total+cost;
cout<<"Cost:";
cout<<cost<<endl;
break;
}elseif (choice_2==2)
{
cout<<"Quantity:";
cin>>quantity;
cout<<endl;
cost=multiply(drinks[1],quantity);
total=total+cost;
cout<<"Cost:";
cout<<cost<<endl;
break;
}elseif (choice_2==3)
{
cout<<"Quantity:";
cin>>quantity;
cout<<endl;
cost=multiply(drinks[3],quantity);
total=total+cost;
cout<<"Cost:";
cout<<cost<<endl;
break;
}
}
}
cout<<"Do you want to try again?(y/n)";
cin>>more;
if (more=='y'||more=='Y')
{
system ("cls");
goto again;
}elseif (more=='n'||more=='N')
{
cout<<"The total amount you bought is "<<total<<endl;
cout<<"Please enter payment:";
cin>>cash;
cout<<endl;
do
{
system ("cls");
cout<<"Sorry the amount you entered is incorrect"<<endl;
cout<<"Please try again"<<endl;
cout<<endl;
cout<<"The total amount you bought is "<<total<<endl;
cout<<"Please enter payment:";
cin>>cash;
cout<<endl;
}while (cash>total);
change=subtract(cash,total);
cout<<"Your change:"<<change<<endl;
}
cout<<"...Please press ENTER..."<<endl;
cin.ignore(2);
return 0;
}
This part of the code still has problems so if anyone has any opinions I'd be very thankfull.
cout<<"Do you want to try again?(y/n)";
cin>>more;
if (more=='y'||more=='Y')
{
system ("cls");
goto again;
}else if (more=='n'||more=='N')
{
cout<<"The total amount you bought is "<<total<<endl;
cout<<"Please enter payment:";
cin>>cash;
cout<<endl;
do
{
system ("cls");
cout<<"Sorry the amount you entered is incorrect"<<endl;
cout<<"Please try again"<<endl;
cout<<endl;
cout<<"The total amount you bought is "<<total<<endl;
cout<<"Please enter payment:";
cin>>cash;
cout<<endl;
}while (cash>total);
change=subtract(cash,total);
cout<<"Your change:"<<change<<endl;
}
Your functions are pretty useless to be honest, subtract and add are a waste of time, and the printing ones are kind of worthless IMHO. You are going 1 too many on your arrays (int something[2] has 2 elements, [0] and [1] ONLY).
Use more indentation, your code is quite difficult to read (at least your main function is).
Don't use goto...it's not needed here. Use a loop. And add some useful functions, like a "Select your choice" function or something.