HELP LOOP!!!

Write your question here.
I have no idea how to get the subtotal with the loop. This is still not Finish btw

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. Finally, it asks the user if the user wants to process more transactions.

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
  #include <iostream.h>
#include <conio.h>

int main()
{
    char repsonse ='Y';
    double price,subtotal,subt,total;
    int code,quantity,response;
   
 
    
    cout<<"=========Welcome to Market========"<<endl;
    cout<<"Code         Product        Price"<<endl;
    cout<<"101          Apple          20.00"<<endl;
    cout<<"102          Orange         25.25"<<endl;
    cout<<"103          Grapes         30.00"<<endl;
    cout<<"104          W.Melon        35.25"<<endl;
    cout<<"105          Mango          30.00"<<endl;
    
    while(code>=101||code<=105)
    {
                               cout<<"\nEnter Code:";
                               cin>>code;
    if((code<101&&code>0)||(code>105))
    {cout<<"Invalid Code!"<<endl;}
    
    
    if(code==101)
    {cout<<"Apple 20.00"<<endl;
    cout<<"Enter Quantity: ";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;}
    price=20.00;
    subt=(quantity*price);
    subtotal=subt;
    cout<<"Subtotal: "<<subtotal<<endl;}
    
    else if(code==102)
    {cout<<"Orange 25.25"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;}
    price=25.25;
    subt=(quantity*price);
    subtotal=subt;
    cout<<"Subtotal: "<<subtotal<<endl;}
    
    else if(code==103)
    {cout<<"Grapes 30.00"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;}
    price=30.00;
    subt=(quantity*price);
    subtotal=subt;
    cout<<"Subtotal: "<<subtotal<<endl;}
    
    else if(code==104)
    {cout<<"W.Melon 35.25"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;}
    price=35.25;
    subt=(quantity*price);
    subtotal=subt;
    cout<<"Subtotal: "<<subtotal<<endl;}
    
    else if(code==105)
    {cout<<"Mango 30.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;}
    price=30.00;
    subt=(quantity*price);
    subtotal=subt;
    cout<<"Subtotal: "<<subtotal<<endl;}
    
    else if(code==0)
    {cout<<"Total: "<<total<<endl;
    total=1000;}
    

}
 getch();
 return 0;
}
Last edited on
...whitespace is your friend. That is one of the beauties of C++. You can put in as much whitespace as you want (assuming in non-string cases here) with no repercussions, compared to whitespace-dependent languages. It won't affect the end result of the code, but at least it'll be legible... next time when you go to look at your code, be a bit less stringent on your uses of the tab (which should be default 4 spaces, simply because it looks better that way) and enter key. It'll help you spot errors more easily when all of your code isn't crammed into eye-gouging boxes.

As for your actual issue... well, for one, I recommend using a switch statement, simply because long changes of if-else statements with integers looks messy in comparison to a nice, concise switch statement. About loops... I recommend that you take a look at the documentation on this site, which explains the beauty of the while loop. In several cases here, while loops would be ideal. Hell, just setting up a function for calculating the subtotal would be a good idea. That, compared to re-stating it each time. However, that's a bit beyond this program, so for now, just look at the documentation. You should also find some info on those aforementioned switches.
Made some changes to your program. Cleaned up some unnecessary statements. Also made it easier through switches incase you have yet to use switch statements.

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
#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
      char response;
    double price,subt;
    int code,quantity;
   
    cout<<"Do you have a customer (Y/N)";
    cin>>response;
    
    while(response=='y || response == 'Y')
{
    cout<<"=========Welcome to Market========"<<endl;
    cout<<"Code         Product        Price"<<endl;
    cout<<"101          Apple          20.00"<<endl;
    cout<<"102          Orange         25.25"<<endl;
    cout<<"103          Grapes         30.00"<<endl;
    cout<<"104          W.Melon        35.25"<<endl;
    cout<<"105          Mango          30.00"<<endl;
    
double total = 0; //allows you to use += to calculate produce subtotal since no tax. 
             //Will also reset total to 0 when you want to serve a new customer.
   do
    {
    cout<<"\nEnter Code:";
	cin>>code;
	switch(code)//the () tells what variable to use and the case are what value will cause that to occur.
	{
	
	case 101:
    cout<<"Apple 20.00"<<endl;
    cout<<"Enter Quantity: ";
    cin>>quantity;
    if(quantity<=0)
	{cout<<"Invalid Quantity"<<endl;
	cin>>quantity;}
    price=20.00;
    subt=(quantity*price);
    cout<<"$"<<subt<<endl;
	break; //breaks from case to continue in function
    
	case 102:
    cout<<"Orange 25.25"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
	{cout<<"Invalid Quantity"<<endl;
	cin>>quantity;}
    price=25.25;
    subt=(quantity*price);
    cout<<"$"<<subt<<endl;
    break;

	case 103:
    cout<<"Grapes 30.00"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
	cin>>quantity;}
    price=30.00;
    subt=(quantity*price);
    cout<<"$"<<subt<<endl;
    break;

	case 104:
    cout<<"W.Melon 35.25"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
	 cin>>quantity;	}
    price=35.25;
    subt=(quantity*price);
	cout<<"$"<<subt<<endl;
    break;

    case 105:
    cout<<"Mango 30.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
	cin>>quantity;}
    price=30.00;
    subt=(quantity*price);
    cout<<"$"<<subt<<endl;
    break;

	case 0://Sentinal value that gives subtotal (because no tax) then ends your function.
    cout<<"Subtotal: "<<total<<endl;
    break;

	default:
		cout<<"Invalid Code!"<<endl;
		break;
	}
		
	total+=subt; //will add all the produce values up. is read as total=total+subt
	}
while (code!=0);//checks if code = 0 at the end of function wont end till it is.
}
cout<<"Do you have anymore customers?(Y/N)";
cin>>response;
} 


if you want to make the quantity keep looping till the cashier enters in a acceptable quantity you can nest a while loop in the quantity statement

which would look like
1
2
3
4
5
6
7
8
9
10
11
case 105:
    cout<<"Mango 30.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    while(quantity<=0)
    {cout<<"Invalid Quantity Please Enter In Valid Quantity 1+"<<endl;
	cin>>quantity;}
    price=30.00;
    subt=(quantity*price);
    cout<<"$"<<subt<<endl;
    break;


if you want a cash tendered portion I will give you a quick skeleton.
you would need two variables added. And this would be inserted into the case 0: portion since that is your sentinal value and values are isolated to the loop/function they are part of.

1
2
3
4
5
6
7
8
output "please enter in amount tendered."
input tender
while((tender-total)<0)
output "Invalid Amount Tendered please Pay Proper Amount"

change = tender-total
output change


But with all the things you want this program to do you should also learn about classes or function to also help make your program easier to follow.
Last edited on
this works. lack of comments in the source code, but at least it studies a little.
should be fully responsive to the track that you provide. I am allowed to add the number of daily sales and daily receipts of the sale.
I lost a bit of time to do some embroidery.
I wanted to post it the other day but I had problems with the internet. I took the opportunity to do some testing.

excuse to text messages in Italian

bye

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

#include <iostream>

using namespace std;
float totale, totalDay, subTotale;
int numCodice=0;
bool test=true;
int numSale=0;

void startScreen()
{
    cout<<"+------------------------------------------------------------------------+"<<'\n';
    cout<<"|            company:         PUSHER FRUIT   2013                        |"<<'\n';
    cout<<"+------------------------------------------------------------------------+"<<'\n';
    cout<<"| COD: 1 = Orange; | COD: 2 = Apple; | COD: 3 = Uva; | COD: 4 = Angurie; |"<<'\n';
    cout<<"+------------------------------------------------------------------------+"<<'\n';    
    cout<<"|                  COD: 0 = end Sum; | COD: 9 = Exit;                    |"<<'\n';
    cout<<"+------------------------------------------------------------------------+"<<'\n';
    
}

void miniscreen()

{
    cout<<"+------------------------------------------------------------------------+"<<'\n';
    cout<<"| COD: 1 = Orange; | COD: 2 = Apple; | COD: 3 = Uva; | COD: 4 = Angurie; |"<<'\n';
    cout<<"+------------------------------------------------------------------------+"<<'\n';
    cout<<"|                  COD: 0 = end Sum; | COD: 9 = Exit;                    |"<<'\n';
    cout<<"+------------------------------------------------------------------------+"<<'\n';
    
}

float calcTotal(float cUnit)
{
    float quantita =0;
    cout<<" ********** input weigth: "<<'\n';
    cin>>quantita;
    subTotale = quantita * cUnit;
    totale+=subTotale;
    totalDay+=totale;
    cout<<" ### totale per prodotto= "<<subTotale<<" euro. ### totale parziale= "<<totale<<" euro "<<'\n';
    return totale;
}


float prodotto(int n_Codice)
{
    float costoUnitario=0;
    while(test)
    {
        switch (n_Codice)
            {
                case 1:
                    {
                        costoUnitario =1.50;
                        cout<<" Arance di Sicilia  "<<costoUnitario<<" euro/kg"<<'\n';
                        break;
                    }
                case 2:
                    {
                        costoUnitario =2.50;
                        cout<<" Mele dell' Alto Adige  "<<costoUnitario<<" euro/kg"<<'\n';
                        break;
                    }                    
                case 3:
                    {
                        costoUnitario =3.00;
                        cout<<" Uva del Salento  "<<costoUnitario<<" euro/kg"<<'\n';
                        break;
                    }
                case 4:
                    {
                        costoUnitario =0.50;
                        cout<<" Anguria del Salento  "<<costoUnitario<<" euro/kg"<<'\n';
                        break;
                    }
                case 0:
                   {
                    cout<<" end products "<<'\n';
                    break;
                   }
                case 9:
                {
                    cout<<" closed programm "<<'\n';
                    test=false;
                    break;
                }
                default:
                    {
                        cout<<" prodotto sconosciuto "<<'\n';
                        //test=false;
                        break;
                    }
            }
        if((test==true)&&((n_Codice > 0)&&(n_Codice<=4)))
             {
                calcTotal(costoUnitario);
                cout<<"+-------------------------+"<<'\n';
                cout<<"| input code product:     |"<<'\n';
                cout<<"+-------------------------+"<<'\n';
                cin>>n_Codice;
                if (n_Codice=='9')
                    numCodice=n_Codice;
             }
        else
            test=false;
    }    
   
    return totalDay;
}

int monetaResto()
{
    float resto=0;
    int modPay=0;
    float contanti=0,subSaldo=0;
    if (totale>0)
    {
        cout<<"+----------------------------------+"<<'\n';
        cout<<"| totale dovuto: "<<totale<<" euro          |"<<'\n';
        cout<<"| scegli la modalità di pagamento: |"<<'\n';
        cout<<"+----------------------------------+"<<'\n';
        cout<<"| 1- Bancomat;                     |"<<'\n';
        cout<<"| 2- Cash;                         |"<<'\n';
        cout<<"| 3- Cash + Bancomat;              |"<<'\n';
        cout<<"+----------------------------------+"<<'\n';
    
    cin>>modPay;
    
    switch(modPay)
    {
        case 2:
        {
            cout<<" inserisci somma versata: "<<'\n';
            cin>>contanti;
            resto = contanti - totale;
            cout<<" resto euro: "<<resto<<'\n';
            numSale++;
            cout<<" ticket n°: "<<numSale<<'\n';
            cout<<" ***** **** *** thanks and goodbye *** **** ***** "<<'\n';
            break;
        }
        case 3:
        {
            cout<<" inserisci  la somma parziale in contanti "<<'\n';
            cin>>contanti;
            subSaldo = totale - contanti;
            cout<<" saldo da versare tramite bancomat: "<<subSaldo<<'\n';
            // fall trough;
        }
        case 1:
        {
            if (subTotale>0)
            {
                cout<<"+-----------------------+"<<'\n';
                cout<<"| inserire PagoBancomat |"<<'\n';
                cout<<"| inserire PIN code:    |"<<'\n';
                cout<<"| ....wait pls.....     |"<<'\n';
                cout<<"| transazione conclusa  |"<<'\n';
                cout<<"+-----------------------+"<<'\n';
                numSale++;
                cout<<" ticket n°: 00"<<numSale<<'\n';
                cout<<" ***** **** *** thanks and goodbye *** **** ***** "<<'\n';
            }
            break;
        }
        default :
        {
            cout<<"+-------------------------+"<<'\n';
            cout<<"|      input cash:        |"<<'\n';
            cout<<"+-------------------------+"<<'\n';
            cin>>contanti;
            resto = contanti - totale;
            cout<<" resto euro: "<<resto<<'\n';
            numSale++;
            cout<<" ticket n°: 00"<<numSale<<'\n';
            cout<<" ***** **** *** thanks and goodbye *** **** ***** "<<'\n';
            break;
        }
            
    }
  }
    
    
    return resto;
    
}


int main()
{
    
    totalDay =0;
        
    while (test)
        {
            bool otherSales = 0;
            startScreen();
                        while (test)
            {
                cout<<"+-------------------------+"<<'\n';
                cout<<"| input code product:     |"<<'\n';
                cout<<"+-------------------------+"<<'\n';
                cin>>numCodice;
                
                if (numCodice=='9')
                    break;
                
                prodotto(numCodice);
                miniscreen();
            }
            if (numCodice =='9')
                break;
            monetaResto();
            
            
            cout<<" continue with sales? (1 = yes, 0 = not) ";
            cin>>otherSales;
            if (otherSales == 1)
            {
                test=true;
                totale=0;
            }
            else
                test=false;           
                        
        }
    cout<<"+-------------------------------+"<<'\n';
    cout<<"| dayli receipts: euro | "<<totalDay<<'\n';
    cout<<"| ticket n°:           | 00"<<numSale<<'\n';
    cout<<"+-------------------------------+"<<'\n';
        return 0;
    }



ps-there are only cases impossible in reality, for example, if you pay in cash, of course, the cash will always be greater than the cost of the fruit (otherwise the fruit, do not deliver the goods);)))

Last edited on
Topic archived. No new replies allowed.