Cashier Program

Can you make my program better? The only problem I have with this is my breakdown. What should I change?

Cashier 2.0 is a C++ program that displays on the screen item codes with corresponding item description and price (at least 10 items). It asks the user to enter the code of the item purchased by the customer. If the user enters a code that is not in the choices, it asks again until he/she enters a valid code. Then, its description and price are displayed on the screen.
Also, it asks for the quantity of the particular item code entered. Again, if the user enters a zero or negative value, it asks again for the quantity until he or she enters a valid value. Afterwards, it displays the subtotal amount. It will keep on asking for an item code, and will process the same until such time that the user enters ‘0’. When the user enters ‘0’, the total amount due is displayed.
Then, it asks the user to tender the amount of cash of the customer. If the amount tendered is less than the amount due, it asks again for the amount until he or she enters a valid amount. Then, it prints the change due to the customer and its breakdown. Finally, it asks the user if he or she wants to process more transactions.
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;


void order();

int main()
{
double centspart;
char ans;
int whole;

int change_1000 = whole / 1000;
int change_500 = whole / 500;
int change_200 = whole / 200;
int change_100 = whole / 100;
int change_50 = whole / 50;
int change_20 = whole / 20;
int change_10 = whole / 10;
int change_5 = whole / 5;
int change_1 = whole / 1;

double change_twentyfivecents;
double change_tencents;
double change_fivecents;

double code, qty, total, change, cash, A = 25.00, O = 30.00, SM = 60.00, S = 3.00;
double GM = 40.00, D = 100.00, M = 60.00, WM = 63.00, PP = 100.00, P = 55.00;
char res;

cout<<"Welcome to Fruit Selection!\n";
cout<<"ITEM CODES DESCRIPTION PRICE\n";
cout<<"101 Apple P25.00\n";
cout<<"102 Orange P30.50\n";
cout<<"103 Sweet Mango P60.00\n";
cout<<"104 Strawberry P3.00\n";//
cout<<"105 Green Mango P40.00\n";
cout<<"106 Durian P100.00\n";
cout<<"107 Melon P60.00\n";
cout<<"108 Watermelon P63.00\n";
cout<<"109 Papaya P100.00\n";
cout<<"110 Pineapple P55.00\n\n";

cout<<"Please enter item code: ";

cin>>code;
if (code == 101)
{
cout<<"\n\nApple\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*A;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity. \n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==102)
{
cout<<"\n\nOrange\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*O;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}
else if (code==103)
{
cout<<"\n\nSweet Mango\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*SM;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==104)
{
cout<<"\n\nStrawberry\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*S;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;

}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==105)
{
cout<<"\n\nGreen Mango\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*GM;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity. \n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient.";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==106)
{
cout<<"\n\nDurian\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*D;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==107)
{
cout<<"\n\nMelon\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*M;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer!";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer!";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon!";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care!";
}

else if (code==108)
{
cout<<"\n\nWatermelon\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*WM;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient.";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==109)
{
cout<<"\n\nPapaya\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*PP;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else if (code==110)
{
cout<<"\n\nPineapple\n";
cout<<"Quantity: ";
cin>>qty;

if (qty>=1)
{
total=qty*P;
cout<<"Total Amount Due: "<<total<<"\n";
}

else
{
cout<<"\nInvalid quantity.\n\n";
order();
return 0;
}
cout<<"Please enter your amount of cash: ";
cin>>cash;
if (cash>=total)
change=cash-total;
else
{
cout<<"\nYour cash is insufficient. ";
return 0;
}
cout<<"Change: "<<change<<"\n";
if (total>=10000)
cout<<"\n\nThanks, Diamond customer! ";
else if (total>=7001 && total<=9999)
cout<<"\n\nNics, Gold customer! ";
else if (total>=2001 && total<=7000)
cout<<"\n\nGreat, see you soon! ";
else if (total>=0 && total<=2000)
cout<<"\n\nTake care! ";
}

else
{
cout<<"The code you entered is invalid. The program will close now.";
return 0;
}
cout<<"Do you want to buy again? (Y/N): ";
cin>> res ;
res=toupper(res);
if(res=='Y')
{
system("cls");
main();
whole = (int)change;

change_1000 = whole / 1000;
change_500 = whole / 500;
change_200 = whole / 200;
change_100 = whole / 100;
change_50 = whole / 50;
change_20 = whole / 20;
change_10 = whole / 10;
change_5 = whole / 5;
change_1 = whole / 1;

change_twentyfivecents = (whole / .25);
change_tencents = whole / 0.10;
change_fivecents = whole / 0.05;

cout<<"\n\n__________BREAKDOWN___________";
cout<<"\n\nDenomination Pieces Amount \n";
cout<<"\n1000\t\t\t"<<change_1000; cout<<"\t\t\t"<<change_1000*1000;
cout<<"\n500\t\t\t"<<change_500; cout<<"\t\t\t"<<change_500*500;
cout<<"\n200\t\t\t"<<change_200; cout<<"\t\t\t"<<change_200*200;
cout<<"\n100\t\t\t"<<change_100; cout<<"\t\t\t"<<change_100*100;
cout<<"\n50\t\t\t"<<change_50; cout<<"\t\t\t"<<change_50*50;
cout<<"\n20\t\t\t"<<change_20; cout<<"\t\t\t"<<change_20*20;
cout<<"\n10\t\t\t"<<change_10; cout<<"\t\t\t"<<change_10*10;
cout<<"\n5\t\t\t"<<change_5; cout<<"\t\t\t"<<change_5*5;
cout<<"\n1\t\t\t"<<change_1; cout<<"\t\t\t"<<change_1*1;


cout<<"\n0.25\t\t\t"<<change_twentyfivecents; cout<<"\t\t\t"<<change_twentyfivecents*.25;
cout<<"\n0.10\t\t\t"<<change_tencents; cout<<"\t\t\t"<<change_tencents*.10;
cout<<"\n0.05\t\t\t"<<change_fivecents; cout<<"\t\t\t"<<change_fivecents*.05;

}
getch();



}

void order()
{

cout<<"Please Enter another code";
system("pause");
system("cls");
main();
}
It is not a valid C++ program. You are not allowed to call main() in your program.

If you have to write something more than once, you did something wrong. Make a subroutine and pass variable stuff to it, or store value of currently chosen element and use it. Do not place huge chains of if statements.

Your program would work better with arrays and standard library usage.

Functions larger than 50 lines should be avoided.

I would expect this program to be no more than 150 lines.
Last edited on
start with this , you just have to ask now the user to enter a valid code ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>

using namespace std;

const size_t MAX_ITEMS = 10;

class Item
{
    string m_desc;
    unsigned int m_price;
    unsigned int m_code;
public:
    Item(string _desc , unsigned int _price , unsigned int _code)
    : m_desc(_desc) , m_price(_price) , m_code(_code)
    {
    }
    ~Item(void)
    {
    }
    void display(void) const
    {
        std::cout << "Price : " << m_price << "$ Desc : " << m_desc << std::endl;
    }
    unsigned int getCode(void)const
    {
        return m_code;
    }

} g_items[MAX_ITEMS] =
{
    {"apple" , 10 , 1112},
    {"banana" , 10 , 1113},
    {"pear" , 10 , 1114},
    {"tomato" , 10 , 1115},
    {"cucumber" , 10 , 1116},
    {"grape" , 10 , 1117},
    {"wine" , 10 , 1118},
    {"juice" , 10 , 1119},
    {"gum" , 10 , 1120},
    {"magazine" , 10 , 1121}
};

bool display(unsigned int _code)
{
    bool foundItem = false;
    for(size_t i = 0 ; i < MAX_ITEMS ; ++ i)
        if( g_items[i].getCode() == _code)
        {
          g_items[i].display();
          foundItem = true;
          break;
        }
    return foundItem;
}
int main(int argc , char * argv[])
{
    display(1112);
    display(1113);
    display(1114);
    display(1115);
    display(1116);
    display(1117);
    display(1118);
    display(1119);
    display(1120);
    display(1121);

 //   system("pause");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.