How will I repeat my whole transaction again? (codes inside)

Write your question here.

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
#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
	int code, quantity;
	double price, subtotal, total, change, cash;
	char response;
	
	cout<<"Item Codes        Description                 Price";     
    cout<<"\n100             Grapes                     125.35";     
    cout<<"\n101             Apples                      52.20";      
    cout<<"\n102             Oranges                     50.25";     
    cout<<"\n103             Pork                       150.20";     
    cout<<"\n104             Beef                       170.25";     
    cout<<"\n105             Chicken                    120.50";     
    cout<<"\n106             Milk                       100.00";     
    cout<<"\n107             Eggs                        60.00";     
    cout<<"\n108             Bread                       70.25";     
    cout<<"\n109             Rice                       150.99"; 
	
	do
	{
		cout<<"\nEnter code : ";
		cin>>code;
		
		switch (code)
		{
		    case 100:
			cout<<"Grapes          125.35\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 125.35;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	        break;
	    
		    case 101:
	    	cout<<"Apples           52.20\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 52.20;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	        break;
	        
			case 102:
	        
			cout<<"Oranges          50.25\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 50.25;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	        break;
	        
	        case 103:    
			cout<<"Pork            150.20\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 150.20;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
            break;
            
	        case 104:   
			cout<<"Beef            170.25\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 170.25;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	    
	        case 105:    
			cout<<"Chicken         120.50\n";
			cout<<"Quantity : ";
			cin>>quantity;
			break;
			
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 120.50;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	    
	        case 106:    
			cout<<"Milk            100.00\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 100.00;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	        break;
	        
	        case 107:
			cout<<"Eggs             60.00\n";
			cout<<"Quantity : ";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 60.00;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
			break;
	         
	        case 108:
			cout<<"Bread            70.25\n";
			cout<<"Quantity : \n";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 70.25;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	        break;
	        
	        case 109:
			cout<<"Apples          150.99\n";
			cout<<"Quantity : \n";
			cin>>quantity;
			if (quantity<=0)
			{
				cout<<"Invalid quantity, please enter again.\n";
				cout<<"Quantity : ";
				cin>>quantity;
			}
			price = 150.99;
			subtotal = quantity*price;
			cout<<"Subtotal : "<<subtotal<<endl;
	        break;
	        
	        case 0:
	        cout<<"Total Amount Due : "<<total<<endl;
	        cout<<"Cash : ";
	        cin>>cash;
	        if (cash<total)
	        {
		        cout<<"\nInsufficient Cash!\n";
		        cout<<"Cash : ";
				cin>>cash;
	        }
	        change = cash - total;
	        cout<<"Change : "<<change;
	        break;
	    
		    default:
		    cout<<"Invalid code, please enter a valid one"<<endl;
		    break;   	 
	    }
	    total+=subtotal;
    }while(code!=0); 
    
    cout<<                     "Thanks for shopping! \n";
    cout<<               "Do you want another transaction? <y/n> ";
    cin>>response;
    if ((response=='Y') || (response=='y'))
//NO IDEA WHAT TO PUT HERE

return 0;
}
Last edited on
Hi carlplusplus, I think for you to understand easily you should divide your code into several functions. You could name those functions as getMenuChoice, getQuantity, newTransaction, that would make your code cleaner and readable.
Topic archived. No new replies allowed.