A sample program in C++... I called it the "Beverage Store"

//Beverage Store
#include<iostream.h>
#include<conio.h>
main()
{
int bev,bill,x,n,tbill,m,ch;
cout << "1 - Water for P20." <<endl;
cout << "2 - Coke for P30." <<endl;
cout << "3 - Sprite for P25." <<endl;
cout << "4 - Root Beer P25." <<endl;
cout << "5 - Iced Tea for P25." <<endl;
cout << "0 - Cancel. " <<endl;
cout << "Please make a choice of your favorite beverage: "<<endl;
cin>>bev;

if (bev==1)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
tbill=20*n;}
else if (bev==2)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
tbill=30*n;}
else if (bev==3)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
tbill=25*n;}
else if (bev==4)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
tbill=25*n;}
else if (bev==5)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
tbill=25*n;}
else if (bev==0)
tbill=0;
else
cout<<endl<<"Please Input a Valid Choice. Thank You."<<endl;

for (x=1;x<=9;x++)
{cout<<endl<<"Anything You Want To Add???"<<endl;
cout << "1 - Water for P20." <<endl;
cout << "2 - Coke for P30." <<endl;
cout << "3 - Sprite for P25." <<endl;
cout << "4 - Root Beer P25." <<endl;
cout << "5 - Iced Tea for P25." <<endl;
cout << "0 - Cancel. " <<endl;
cout << "9 - None. "<<endl;
cout << "Please make a choice of your favorite beverage: "<<endl;
cin>>bev;

if (bev==1)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
bill=20*n;
tbill+=bill;}
else if (bev==2)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
bill=30*n;
tbill+=bill;}
else if (bev==3)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
bill=25*n;
tbill+=bill;}
else if (bev==4)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
bill=25*n;
tbill+=bill;}
else if (bev==5)
{cout<<"How many items would you like to purchase?"<<endl;
cin>>n;
bill=25*n;
tbill+=bill;}
else if (bev==0)
{bill=0;
tbill+=bill;}
else if (bev==9)
x=10;
else
cout<<"Please Input a Valid Choice. Thank You."<<endl;}


cout<<endl<<"How much is your money?"<<endl;
cin>>m;
if (m<tbill)
cout<<endl<<"Sorry! You have an insufficient amount of money."<<endl<<"Please pay the right amount immediately. Thank You!."<<endl;
else
{cout<<endl<<"I recieve "<<m<<" Pesos"<<endl;
cout<<endl<<"Your Total Bill is = "<<tbill<<" Pesos."<<endl;
ch=m-tbill;
cout<<endl<<"Your Change is = "<<ch<<" Pesos."<<endl;
cout<<endl<<"Thank You Come Again!!!"<<endl;}
getch();
}


Please comment for suggestions,questions and reactions...Thank You.. ;)
Last edited on
first of all, this is hell a lot for reading...

now some suggestions. for each bev value, as the only difference it is causing is the multiplier for n items, it's not wise to explicitly use if...else... clause repeatedly. one way i can think of, through it into an array. the index is the number people choose, that's bev. for each member in the array, align index with values desired. you can eliminate the whole if...else... routine to a few lines.

another thing i noticed, you have the whole "purchase" routine done twice. put it in a loop. you only have to code once then.

[EDIT] the above suggestions does not only make your program looks better, it also make modification easier. assume you an extra choice to add, you just have to add an element to the array and some corresponding print out. in your case, the code you have here, that's a lot work for an extra bev. also, if you need to allow more than once for adding on top of the bill, a loop allows you to change only the iteration count value. one value does the trick instead of copying the whole chunk n times.
Last edited on
You should validate input, it makes your programs more stable and in this case can cut down on a lot of code too. For instance selecting a beverage, assuming that the max that can be purhcased is 10, could be cut down from that huge list of if statements to something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int bev = -1;
while ( bev < 0 || bev > 10 )
{
        cout << "Please make a selection, enter a number between 1 and 9\n";
        cin >> bev;
}

int num = -1;
while ( num <0 || num > 10 )
{
 cout << "how many items would you like to buy? Enter a number between 1 and 10\n"
 cin >> num;
}

Last edited on
Topic archived. No new replies allowed.