A minor problem with a menu code

There's really no errors in this code, but I do want to make it better. As seen below in the code, I have the customer enter in "5 0" to end the code, is there a way to just let them enter just one number instead of two? Thanks!

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
240
241
242
243
244
245
246
  //menu
#include <iostream>    //Important stuff
#include <iomanip>
#include <string>
using namespace std;

int main ()     //Defining variables for codes to work
{
	int a,
		b,
		c,
		d,
		e,
		item1,
		item2,
		item3,
		item4,
		item5,
		w = 0,
		x = 0,
		y = 0,
		z = 0;
	const double Salad1 = 4.50,
		Soup1 = 8.50,
		Chicken1 = 11.00,
		Cheesecake1 = 5.00,
		tax = 1.09;
		double total1,
		total2,
		total3,
		total4,
		total5;

	//Having the customer pick how many of something they want
	//First we tell them what we have and we ask them what they want and how many
	//Then we use cin as a way to plug in the number to use later for the bill.
	cout << "Welcome to CSUF dinings, here is our menu." << endl;
	cout << "1. Salad - $4.50" << endl; 
	cout << "2. Soup - $8.50" << endl;
	cout << "3. Chicken - $11.00" << endl;
	cout << "4. Cheesecake - $5.00" << endl;
	cout << "Enter in the number of the item you would like to order. And how many of it would you like?" << endl;
	cin >> item1 >> a;
	if (item1 == 1)
	{total1 = Salad1*a;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1 << "." << endl;
		w = a;
	}
	else if (item1 == 2)
	{total1 = Soup1*a;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1 << "." << endl;
		x = a;
	}
	else if (item1 == 3)
	{total1 = Chicken1*a;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1 << "." << endl;
		y = a;
	}
	else if (item1 == 4)
	{total1 = Cheesecake1*a;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1 << "." << endl;
		z = a;
	} 
	cout << endl;

	cout << "What else would you like? Enter in the number of the item you would like to order. And how many of it would you like? If you are done ordering, enter in 5 and 0." << endl;
		cout << "1. Salad - $4.50" << endl; 
	cout << "2. Soup - $8.50" << endl;
	cout << "3. Chicken - $11.00" << endl;
	cout << "4. Cheesecake - $5.00" << endl;
	cin >> item2 >> b;
	if (item2 == 1)
	{total2 = Salad1*b;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2 << "." << endl;
		w += b;
	}
	else if (item2 == 2)
	{total2 = Soup1*b;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2 << "." << endl;
		x += b;
	}
	else if (item2 == 3)
	{total2 = Chicken1*b;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2 << "." << endl;
		y += b;
	}
	else if (item2 == 4)
	{total2 = Cheesecake1*b;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2 << "." << endl;
		z += b;
	} 
	else if (item2 == 5)
		{
			cout << endl;
			if (w == 1)
	{cout << "You ordered " << w << " salad." << endl;
	}
		if (w >= 2)
		{ cout << "You ordered " << w << " salads." << endl;
	}
		if (x == 1)
		{cout << "You ordered " << x << " soup." << endl;
		}
		if (x >= 2)
		{cout << "You ordered " << x << " soups." << endl;
		}
		if (y == 1)
		{cout << "You ordered " << y << " chicken." << endl;
		}
		if (y >= 2)
		{cout << "You ordered " << y << " chickens." << endl;
		}
		if (z == 1)
		{cout << "You ordered " << z << " cheesecake." << endl;
		}
		if (z >= 2)
		{cout << "You ordered " << z << " cheesecakes." << endl;
		}
		cout << "Your total will be $" << setprecision(2) << fixed << (total1)*tax << "." << endl;
	cout << "Thank you for ordering at CSUF dinings, and have a nice day!" << endl;
	
	return 0; 
	}
	cout << endl;

	cout << "What else would you like? Enter in the number of the item you would like to order. And how many of it would you like? If you are done ordering, enter in 5 and 0." << endl;
		cout << "1. Salad - $4.50" << endl; 
	cout << "2. Soup - $8.50" << endl;
	cout << "3. Chicken - $11.00" << endl;
	cout << "4. Cheesecake - $5.00" << endl;
	cin >> item3 >> c;
	if (item3 == 1)
	{total3 = Salad1*c;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3 << "." << endl;
		w += c;
	}
	else if (item3 == 2)
	{total3 = Soup1*c;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3 << "." << endl;
		x += c;
	}
	else if (item3 == 3)
	{total3 = Chicken1*c;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3 << "." << endl;
		y += c;
	}
	else if (item3 == 4)
	{total3 = Cheesecake1*c;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3 << "." << endl;
		z += c;
	} 
	else if (item3 == 5)
	{
		cout << endl;
		if (w == 1)
	{cout << "You ordered " << w << " salad." << endl;
	}
		if (w >= 2)
		{ cout << "You ordered " << w << " salads." << endl;
	}
		if (x == 1)
		{cout << "You ordered " << x << " soup." << endl;
		}
		if (x >= 2)
		{cout << "You ordered " << x << " soups." << endl;
		}
		if (y == 1)
		{cout << "You ordered " << y << " chicken." << endl;
		}
		if (y >= 2)
		{cout << "You ordered " << y << " chickens." << endl;
		}
		if (z == 1)
		{cout << "You ordered " << z << " cheesecake." << endl;
		}
		if (z >= 2)
		{cout << "You ordered " << z << " cheesecakes." << endl;
		}
		cout << "Your total will be $" << setprecision(2) << fixed << (total1+total2)*tax << endl;
	cout << "Thanks for ordering at CSUF dinings, and have a nice day!" << endl;

	return 0;
	}
	cout << endl;

		cout << "What else would you like? Enter in the number of the item you would like to order. And how many of it would you like? If you are done ordering, enter in 5 and 0." << endl;
		cout << "1. Salad - $4.50" << endl; 
	cout << "2. Soup - $8.50" << endl;
	cout << "3. Chicken - $11.00" << endl;
	cout << "4. Cheesecake - $5.00" << endl;
	cin >> item4 >> d;
	if (item4 == 1)
	{total4 = Salad1*d;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3+total4 << "." << endl;
		w += d;
	}
	else if (item4 == 2)
	{total4 = Soup1*d;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3+total4 << "." << endl;
		x += d;
	}
	else if (item4 == 3)
	{total4 = Chicken1*d;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3+total4 << "." << endl;
		y += d;
	}
	else if (item4 == 4)
	{total4 = Cheesecake1*d;
		cout << "Your total so far will be $" << setprecision(2) << fixed << total1+total2+total3+total4 << "." << endl;
		z += d;
	} 
	else if (item4 == 5)
	{
		cout << endl;
		if (w == 1)
	{cout << "You ordered " << w << " salad." << endl;
	}
		if (w >= 2)
		{ cout << "You ordered " << w << " salads." << endl;
	}
		if (x == 1)
		{cout << "You ordered " << x << " soup." << endl;
		}
		if (x >= 2)
		{cout << "You ordered " << x << " soups." << endl;
		}
		if (y == 1)
		{cout << "You ordered " << y << " chicken." << endl;
		}
		if (y >= 2)
		{cout << "You ordered " << y << " chickens." << endl;
		}
		if (z == 1)
		{cout << "You ordered " << z << " cheesecake." << endl;
		}
		if (z >= 2)
		{cout << "You ordered " << z << " cheesecakes." << endl;
		}
		cout << "Your total will be $" << setprecision(2) << fixed << (total1+total2+total3)*tax << endl;
	cout << "Thanks for ordering at CSUF dinings, and have a nice day!" << endl;

	return 0;
	}
	return 0;
}
It's seems like huge but you can do it by using switch , i suggest you to use switch case according to your display..

you can do like ,In switch other than number in choice it will terminate the loop .

There is a lot of repeated code in your program. Any time you see repeated code, that is a good indication you should move that code into a function.

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
#include <iostream>    
#include <iomanip>
#include <string>
using namespace std;

const double Salad1 = 4.50,
    Soup1 = 8.50,
    Chicken1 = 11.00,
    Cheesecake1 = 5.00,
    tax = 1.09;

void menu ()
{  	cout << "1. Salad - $4.50" << endl; 
	cout << "2. Soup - $8.50" << endl;
	cout << "3. Chicken - $11.00" << endl;
	cout << "4. Cheesecake - $5.00" << endl;
	cout << "5. Done" << endl;
}

//    Note:  If the user selects an item that was previous selected, the qty will be replaced.
/     The other alternative is to make the qty cumulative.
//
bool get_item (int items[], double & total)
{   int     qty;
    int     item;
    bool    repeat;
    
    do  //  Loop until we have valid input
    {   repeat = false;
        cout << "Enter in the number of the item you would like to order. And how many of it would you like?" << endl;
        cin >> item;
        if (item == 5)
            return false;   //  Done - Don;t continue
        cin >> qty;        
        switch (item)
        {
        case 1: total += Salad1 * qty; break;
        case 2: total += Soup1 * qty; break;
        case 3: total += Chicken1 * qty; break;
        case 4: total += Cheesecake1 * qty; break;
	default: cout << "Item not recognized" << endl;
	             menu ();
	             repeat = true;     //  Prompt again
	}    
    } while (repeat);
    cout << "Your total so far will be $" << setprecision(2) << fixed << total << "." << endl;	
    cout << "What else would you like?" << endl;
    cout << endl;   		
    items[item-1] = qty;
    return true;        //  Prompt for another item
}

void display_order (int items[], double total)
{   if (items[0] == 1)
	    cout << "You ordered " << items[0] << " salad." << endl;
	if (items[0] >= 2)
		 cout << "You ordered " << items[0] << " salads." << endl;
	if (items[1] == 1)
		cout << "You ordered " << items[1] << " soup." << endl;
	if (items[1] >= 2)
		cout << "You ordered " << items[1] << " soups." << endl;
	if (items[2] == 1)
		cout << "You ordered " << items[2] << " chicken." << endl;
	if (items[2] >= 2)
		cout << "You ordered " << items[2] << " chickens." << endl;
	if (items[3] == 1)
		cout << "You ordered " << items[3] << " cheesecake." << endl;
	if (items[3] >= 2)
		cout << "You ordered " << items[3] << " cheesecakes." << endl;		
    cout << "Your total is $" << setprecision(2) << fixed << total * tax << "." << endl;			
}
			
int main ()    
{  int items[4] = {0};  // Initialize the qty of each item to 0
    double total = 0.0;

    cout << "Welcome to CSUF dinings, here is our menu." << endl;
    menu ();	
    while (get_item (items, total))
	    ;      
    display_order (items, total);
    cout << "Thanks for ordering at CSUF dinings, and have a nice day!" << endl;
    system ("pause");
    return 0;
}


Last edited on
This code worked out nicely! But can you explain how bool, repeats, voids, and breaks work? Thanks! Also I do want the qty cumulative, how do you do that?
Last edited on
bool is a built in type that has two possible values: true or false. get_item uses a bool return value to determine if the loop at line 79 should continue or not. If get_item returns true, get_item gets called again. if get_item returns false, the while condition is not satisfied and the loop ends.

repeat is used to control the loop from lines 28-44. The purpose of the loop is to check that we have valid input. If we don't have valid input, we set repeat to true, and the will execute another iteration of the loop. If we have valid input, then repeat remains false, and the loop terminates.

void says a function returns nothing.

If you don;t use break in a switch statement, you will fall through to the next case.

I do want the qty cumulative, how do you do that?

I'll let you make an attempt to do that. Hint: Look at line 49
Hmm, if that's the case, why would you put repeat = false before you tell it that item = 5 is false? And what does return 0 has to do there?

Also, why would you code it items[item-1]? Is there a difference if you just code it into a number? Or set it to just items[item]?

And I tried qty += qty, it didn't work :/

Sorry for the many questions, but why would items[4] come after int main?

For line 74, you only set items[4] to 0, how did that initialize all numbers to 0? And for it, why did you use { rather than [? Or without brackets rather?

But I really appreciate your help, thanks :)
Last edited on
why would you put repeat = false before you tell it that item = 5 is false?

At line 29, we set repeat to false at the top of the loop. We're going to assume the user entered valid data. In the default branch of the switch statement, we set repeat to true if we detect invalid data. We could have set repeat to true initially, but then we would have had to set it to false in each of the 4 valid branches in the switch statement. Easier to change it just once.

We could have put the repeat = false after the test for 5. That would not change the logic. Just a matter of style to set variables at the top of the loop before any transfers of control. Less chance of possible logic errors.

The do/while loop is not actually needed. We could remove the loop by changing line 43 to return true; That would cause us to call get_item again. Again a matter of style. I prefer not to return from get_item until I actually have an item or no more are wanted (two conditions). The loop at line 79 current has a null action, but if we were to add code there, we might have to distinguish between whether get_item actually obtained an item or not, or no more are wanted (three conditions). The do/while loop is a common idiom for checking for valid input and is something you should learn, which is why I used it.

Also, why would you code it items[item-1]? Is there a difference if you just code it into a number? Or set it to just items[item]?

The user enters an item number from 1 to 4. However, the elements in our array from 0-3, therefore we subtract 1 to refer to the correct element.

And I tried qty += qty, it didn't work

That would just double the qty the user entered. Again, look at line 49. How would you change it so that you're adding qty to the value already in the array?

why would items[4] come after int main?

You have to declare the items array somewhere. The choices are global or local to main. Declaring globals is poor style. Variables should always be as local as possible.

For line 74, you only set items[4] to 0, how did that initialize all numbers to 0? And for it, why did you use { rather than [?
 
int items[4] = {0};

That line has two parts. The left side of the = declares an array of 4 ints. The right side of the array is initialization syntax. The {0} (with braces) says to initialize the array of its declared size to zeroes.




Oh okay I see; in class, we just haven't learned any loop statements yet, so I have a lot of questions. As for the arrays, does it always default set it to 0-3? Or can we change it to items[item] and then items[1] = salad, items[2]=soups etc, pretty much just skipping out 0 is what I mean. As for the cumulative quantity, I figured it out, I just didn't realize that the numbers were stored into the arrays, thanks :)
does it always default set it to 0-3?

As I said before, the = {0} initialization syntax sets the entire array to 0.
Or can we change it to items[item]

No. item is the item number the user entered. You don't want to declare the dimension of an array based on the item number the user entered. Besides, variable array dimensions are not legal in C++.
and then items[1] = salad

You could do that to avoid subtracting 1 from item. You would have to declare items as int items[5]; to allow you to reference entries 1-4 and ignore the 0th entry.





Last edited on
Topic archived. No new replies allowed.