Stuck in infinite loop

I'm working on an assignment for my c++ class and can't seem to get out of a loop. The assignment is as follows:

Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:
a. Show the customer the different breakfast items offered by the restaurant.
b. Allow the customer to select more than one item from the menu
c. Calculate and print the bill

Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):

Plain Egg $1.45
Bacon and Egg $2.45
Muffin $0.99
French Toast $1.99
Fruit Basket $2.44
Cereal $0.69
Coffee $0.50
Tea $0.75

First, Define a struct, menuItemType, with two components: menuItem of type string and menuPrice of type double. Then use an array, call it menuList, of the struct menuItemType. Your program must contain the following functions:
• Function getData: This function loads the data into the array menulist.
• Function showMenu: This function shows the different items offered by the restaurant and tells the user how to select the items.
• Function printCheck: This function calculates and prints the check. (Note that the billing amount should include a 5% tax)

Sample output:

Welcome to Johnny’s Restaurant
Bacon and Egg $2.45
Muffin $0.99
Coffee $0.50
Tax $0.20
Amount Due $4.14


And I'm currently on the next exercise which states:

Redo Exercise 4 so that the customer can select multiple items of a particular type. A sample output in this case is:

Welcome to Johnny's Restaurant
1 Bacon and Egg $2.45
2 Muffin $1.98
1 Coffee $0.50
Tax $0.25
Amount Due $5.18


The program displays the menu to me, allows me to select a menu option, prompts me for the quantity, but when I enter '0' which is supposed to print the check, it does absolutely nothing. Any shove in the right direction is greatly appreciated!

Here is what I have:


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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const double SALESTAX = .05;
double totalAmount;

struct menuItemType
{
	char menuItem[20];
	double menuPrice, quantity;
}menuList[8], itemQuantity[8], customerOrderList[8];

void getData(menuItemType menuList[8]);
void showMenu(menuItemType menuList[8], menuItemType customerOrderList[8], menuItemType itemQuantity[8], int counter, int menuSelection);
void printCheck(menuItemType customerOrderList[8], menuItemType itemQuantity[8], int counter, const int SALESTAX, double totalAmount);

int main()
{
	int counter = 0;
	int	menuSelection = ' ';
	double totalAmount = 0;

	getData(menuList);
	showMenu(menuList, customerOrderList, itemQuantity, counter, menuSelection);
	printCheck(customerOrderList, itemQuantity, counter, SALESTAX, totalAmount);

	return 0;
}

void getData(menuItemType menuList[8])
{
	strcpy(menuList[0].menuItem,"Plain Egg");
	menuList[0].menuPrice = 1.45;
	strcpy(menuList[1].menuItem,"Bacon and Egg");
	menuList[1].menuPrice = 2.45;
	strcpy(menuList[2].menuItem,"Muffin");
	menuList[2].menuPrice = 0.99;
	strcpy(menuList[3].menuItem,"French Toast");
	menuList[3].menuPrice = 1.99;
	strcpy(menuList[4].menuItem,"Fruit Basket");
	menuList[4].menuPrice = 2.49;
	strcpy(menuList[5].menuItem,"Cereal");
	menuList[5].menuPrice = 0.69;
	strcpy(menuList[6].menuItem,"Coffee");
	menuList[6].menuPrice = 0.50;
	strcpy(menuList[7].menuItem,"Tea");
	menuList[7].menuPrice = 0.75;
}

void showMenu(menuItemType menuList[8], menuItemType customerOrderList[8], menuItemType itemQuantity[8], int counter, int menuSelection)
{
	cout << "Enter the number of your menu item selections one at a time. When you have completed your order, enter 0 to print your check.";
	cout << endl;
	cout << "1 - Plain Egg" << setw(14) << "$1.45";
	cout << endl;
	cout << "2 - Bacon and Egg" << setw(10) << "$2.45";
	cout << endl;
	cout << "3 - Muffin" << setw(17) << "$0.99";
	cout << endl;
	cout << "4 - French Toast" << setw(11) << "$1.99";
	cout << endl;
	cout << "5 - Fruit Basket" << setw(11) << "$2.49";
	cout << endl;
	cout << "6 - Cereal" << setw(17) << "$0.69";
	cout << endl;
	cout << "7 - Coffee" << setw(17) << "$0.50";
	cout << endl;
	cout << "8 - Tea" << setw(20) << "$0.75";
	cout << endl;

	do
	{
		int menuSelection;

		cin >> menuSelection;

		switch(menuSelection)
		{
		case 1:
			cout << "Enter quantity of " << "'" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 2:
			cout << "Enter quantity of " << "'" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 3:
			cout << "Enter quantity of '" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 4:
			cout << "Enter quantity of '" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 5:
			cout << "Enter quantity of '" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 6:
			cout << "Enter quantity of '" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 7:
			cout << "Enter quantity of '" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 8:
			cout << "Enter quantity of '" << menuList[menuSelection-1].menuItem << "'";
			cout << endl;
			cin >> itemQuantity[menuSelection-1].quantity;
			break;
		case 0:
			break;
		default:
			cout << "Enter the number of a menu item 1-8 or enter 0 to continue. ";
			cout << endl;
		}
	}
	while(menuSelection != 0);

	if(menuSelection >= 0 && menuSelection <=8)
	{
		strcpy(customerOrderList[counter].menuItem,menuList[menuSelection-1].menuItem);
		customerOrderList[counter].menuPrice = menuList[menuSelection-1].menuPrice;
		counter++;
	}

	else if(menuSelection == 0)
	{
		cout << "Your order is complete. ";
		cout << endl;
	}
}

void printCheck(menuItemType customerOrderList[8], menuItemType itemQuantity[8], int counter, const int SALESTAX, double totalAmount)
{
	totalAmount = customerOrderList[0].menuPrice + customerOrderList[1].menuPrice + customerOrderList[2].menuPrice + customerOrderList[3].menuPrice + customerOrderList[4].menuPrice + customerOrderList[5].menuPrice + customerOrderList[6].menuPrice + customerOrderList[7].menuPrice;

	cout << "Welcome to Johnny's Restaraunt";
	cout << endl;

	for(counter=0;counter<8;counter++)
	{
		if(itemQuantity[counter].quantity = 0)
		{

		}

		else
		{
			switch(counter)
			{
			case 0:
				cout << itemQuantity[0].quantity << setw(2) << customerOrderList[0].menuItem << setw(14) << "$" << customerOrderList[0].menuPrice << endl;
			case 1:
				cout << itemQuantity[1].quantity << setw(2) << customerOrderList[1].menuItem << setw(10) << "$" << customerOrderList[1].menuPrice << endl;
			case 2:
				cout << itemQuantity[2].quantity << setw(2) << customerOrderList[2].menuItem << setw(17) << "$" << customerOrderList[2].menuPrice << endl;
			case 3:
				cout << itemQuantity[3].quantity << setw(2) << customerOrderList[3].menuItem << setw(11) << "$" << customerOrderList[3].menuPrice << endl;
			case 4:
				cout << itemQuantity[4].quantity << setw(2) << customerOrderList[4].menuItem << setw(11) << "$" << customerOrderList[4].menuPrice << endl;
			case 5:
				cout << itemQuantity[5].quantity << setw(2) << customerOrderList[5].menuItem << setw(17) << "$" << customerOrderList[5].menuPrice << endl;
			case 6:
				cout << itemQuantity[6].quantity << setw(2) << customerOrderList[6].menuItem << setw(17) << "$" << customerOrderList[6].menuPrice << endl;
			case 7:
				cout << itemQuantity[7].quantity << setw(2) << customerOrderList[7].menuItem << setw(20) << "$" << customerOrderList[7].menuPrice << endl;
			}
		}
	}

		cout << "Tax" << setw(19) << "$" << (totalAmount * SALESTAX) << endl;
		cout << "Amount Due" << setw(12) << "$" << (totalAmount + (totalAmount * SALESTAX));
}
Get rid of line 76.
To elaborate on Duoas answer, you actually have two variables by the name of menuSelection.

One is the variable from the function's input arguments (say mS1) and the other that you have declared within the body of the do-while loop (say mS2).

Within the do-while loop, the mS1 is not visible because mS2 hides it. Hence any changes that you do (read via cin) is made to mS2 and not mS1.

However, the loop terminates when mS1 == 0. This is because here mS2 is not in scope and so mS1 is accessed.

Thus to prevent this confusing interference amongst the variables, remove the second declaration from your code. Then mS1 shall be visible throughout your function.
Great, thanks. I see my mistake as soon as you pointed it out. Sometimes it just takes a fresh set of eyes! Much appreciated.
Topic archived. No new replies allowed.