Variable value not resetting after loop

Hello
i am new to c++ and i'm trying to do a program for a restaurant cashier machine and i'm having a problem with the looping.
basically the program is supposed to accept more than one order and after the order is complete it shows the total amount to be paid by the customer, but the problem is that the variable for the total amount is not resetting during the loop but it's adding the amount from the previous order as well.

How can i reset the value of pay_cash and pay_credit after each order?

I know my code is all over the place and messy but bare with me for a while.
(username: cashier)

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <string>
using namespace std;
int login();
string username,customer;;
float overall_sales;
float manager();
float meal_price,total_meal_price=0;
float dessert_price,total_dessert_price=0;
float drink_price,total_drink_price=0;
float total_price_cash=0,total_price_credit=0;
int pay_option,login_option;
float total_sales=0;
int categories();
char option_menu;
float meals_menu();
float dessert_menu();
float drinks_menu();
int pay_method(); //Prototype of payment methods


int main()
{
    login();

    do
    {
    if (username == "cashier")
    {
        cout<<"You are logged in as a cashier"<<endl;
        cout<<"Enter customer's name"<<endl;
        cin>>customer;
        float pay_cash=0,pay_credit=0;
        categories();
        pay_method();
    }
    else if (username == "manager")
        manager();
    else
    {
      cout<<"Invalid input"<<endl;

    return 0;
    }
    }while (login_option == 1);

   return 0;
 }

int login()
{
    cout<<"Enter username and password"<<endl;
    cout<<"Username: ";
    getline(cin, username);
    if (username =="manager")
    {
        cout<<"You are logged in as a manager"<<endl;
        manager();
    }
}

int categories()
{
    int option;
    cout<<"  Menu items: "<<endl;
    cout<<"1. Main meals"<<endl;
    cout<<"2. Desserts"<<endl;
    cout<<"3. Drinks and beverages"<<endl;
    cin>>option;
    switch (option)
    {
        case 1:meals_menu();break;
        case 2:dessert_menu();break;
        case 3:drinks_menu();break;
    }


}

float meals_menu()//Main meals menu
{
    int main_meal,meal_quantity;
    char option_meal;

    do
    {
        cout<<"Main meals"<<endl;
        cout<<"meal 1 (8.50)"<<endl;
        cout<<"meal 2 (10.00)"<<endl;
        cout<<"meal 3 (12.00)"<<endl;
        cout<<"meal 4 (15.30)"<<endl;
        cout<<"meal 5 (11.50)"<<endl;
        cout<<"meal 6 (6.70)"<<endl;
        cin>>main_meal; //Choosing meals
        cout<<"Enter quantity"<<endl;
        cin>>meal_quantity; //Meal quantity
    switch (main_meal) //Calculating Meal price
        {
        case 1: meal_price = 8.50 * meal_quantity;break;
        case 2: meal_price = 10.00 * meal_quantity;break;
        case 3: meal_price = 12.00 * meal_quantity;break;
        case 4: meal_price = 15.30 * meal_quantity;break;
        case 5: meal_price = 11.50 * meal_quantity;break;
        case 6: meal_price = 6.70 * meal_quantity;break;
        default:
            cout<<"\nInvalid input"<<endl;
        }
        cout<<"Would you like to choose another meal? (Y/N)"<<endl;
        cin>>option_meal;
        total_meal_price = total_meal_price + meal_price;
    }
    while(option_meal == 'Y' || option_meal == 'y');

    cout<<"Would you like to choose something else from the menu? (Y/N)"<<endl;
    cin>>option_menu;
    if (option_menu == 'Y' || option_menu == 'y')
        categories();
}

float dessert_menu()
{
    int dessert,dessert_quantity;
    char option_dessert;

    do
    {
        cout<<"Dessert"<<endl;
        cout<<"dessert 1 (8.00)"<<endl;
        cout<<"dessert 2 (4.50)"<<endl;
        cout<<"dessert 3 (5.50)"<<endl;
        cout<<"dessert 4 (8.30)"<<endl;
        cout<<"dessert 5 (9.00)"<<endl;
        cin>>dessert; //Choosing desserts
        cout<<"Enter quantity"<<endl;
        cin>>dessert_quantity; //Desert quantity
        switch (dessert) //Calculating Desert price
        {
        case 1: dessert_price = 8.00 * dessert_quantity;break;
        case 2: dessert_price = 4.50 * dessert_quantity;break;
        case 3: dessert_price = 5.50 * dessert_quantity;break;
        case 4: dessert_price = 8.30 * dessert_quantity;break;
        case 5: dessert_price = 9.00 * dessert_quantity;break;
        default:
            cout<<"\nInvalid input"<<endl;
        }
        cout<<"Would you like to choose another dessert? (Y/N)"<<endl;
        cin>>option_dessert;
        total_dessert_price = total_dessert_price + dessert_price;
    }
    while (option_dessert =='Y' || option_dessert == 'y');

    cout<<"Would you like to choose something else from the menu? (Y/N)"<<endl;
    cin>>option_menu;
    if (option_menu == 'Y' || option_menu == 'y')
        categories();
}

float drinks_menu()
{
    int drink,drink_quantity;
    char option_drink;

    do
    {
        cout<<"Drinks and beverages"<<endl;
        cout<<"drink 1 (2.50)"<<endl;
        cout<<"drink 2 (5.00)"<<endl;
        cout<<"drink 3 (6.50)"<<endl;
        cout<<"drink 4 (6.50)"<<endl;
        cout<<"drink 5 (7.00)"<<endl;
        cin>>drink; //Choosing drinks
        cout<<"Enter quantity"<<endl;
        cin>>drink_quantity; //Drink quantity
        switch (drink) //Calculating Drink price
        {
        case 1: drink_price = 2.50 * drink_quantity;break;
        case 2: drink_price = 5.00 * drink_quantity;break;
        case 3: drink_price = 6.50 * drink_quantity;break;
        case 4: drink_price = 6.50 * drink_quantity;break;
        case 5: drink_price = 7.00 * drink_quantity;break;
        case 6: drink_price = 8.50 * drink_quantity;break;
        default:
            cout<<"\nInvalid input"<<endl;
        }
        cout<<"Would you like to choose another drink? (Y/N)"<<endl;
        cin>>option_drink;

        total_drink_price = total_drink_price + drink_price;
    }
    while (option_drink == 'Y' || option_drink =='y');

    cout<<"Would you like to choose something else from the menu? (Y/N)"<<endl;
    cin>>option_menu;
    if (option_menu == 'Y' || option_menu == 'y')
        categories();
}

int pay_method()
{
    float total_price_cash=0,total_price_credit=0;
    float pay_cash=0,pay_credit=0;
    cout<<"Choose payment method"<<endl;
    cout<<"1. Cash (Service charge - 3%)\n2. Credit card (Service charge - 5%)"<<endl;
    cin>>pay_option;

    if (pay_option == 1) // Payment by Cash (3% service charge AND 6% GST)
    {
        total_price_cash = total_meal_price + total_dessert_price + total_drink_price;
        pay_cash = total_price_cash + (total_price_cash * 0.03) + (total_price_cash * 0.06);
        cout<<"Your total is (including 6% GST): RM"<<pay_cash<<endl;
    }
    else if (pay_option == 2)// Payment by Credit card (5% service charge AND 6% GST)
    {
        total_price_credit = total_meal_price + total_dessert_price + total_drink_price;
        pay_credit = total_price_credit + (total_price_credit * 0.05) + (total_price_credit * 0.06);
        cout<<"Your total is (Including 6% GST): RM"<<pay_credit<<endl;
    }
    else
    {
        cout<<"Invalid input"<<endl;

        total_sales = pay_cash + pay_credit;

        cout<<"Would you like to continue as a cashier or manager? ( 1 = Cashier)( 2 = Manager)"<<endl;
        cin>>login_option;
    }

    cout<<"Would you like to continue as a cashier or manager? ( 1 = Cashier)( 2 = Manager)"<<endl;
    cin>>login_option;

}

    float manager()
    {
        cout<<"Number of customers: "<<endl;
        cout<<"Total sales: "<<total_sales<<endl;
        cout<<"Would you like to continue as a cashier or manager? ( 1 = Cashier)( 2 = Manager)"<<endl;
        cin>>login_option;
    }
None of your functions return anything. They all say they return something but none of them do. This is really really bad.

1
2
3
        cin>>customer;
        float pay_cash=0,pay_credit=0;
        categories();

That line in the middle is creating NEW variables named pay_cash and pay_credit, and setting their value to zero. If you want to change the value of the existing global variables don't create new variables.

1
2
3
4
        cin>>customer;
        pay_cash=0;
        pay_credit=0;
        categories();


Also, don't use global variables. They're a really bad idea.
The reason i'm using global variables is because i need to use them in different functions.

So pass those variables into functions as arguments.

Global variables are, generally, a bad idea, and are likely to cause you problems at some points.
Topic archived. No new replies allowed.