Loop not working

closed account (4wvoLyTq)
The issue seems to be when I go into the menu it works the first time (say you hit pasta) but then when I want to order something else if I choose the same option it goes straight to "Do you want to order something else, rather than the food menu". Any suggestions

source.cpp file

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
247
248
#include "Mainmenu.h"
#include "logout.h"
#include <iostream>

int main()
{
	Mainmenu* mmenu = new Mainmenu;
	Logout* lout = new Logout;

	char answer;
	std::cout << "Would you like to place an order? (y/n)\n" << endl;
	std::cin >> answer;

	if (answer == 'y' || answer == 'Y')
	{
		mmenu->menu();
	}
	else if (answer == 'n' || answer == 'N')
	{
		lout->exit();
	}
	else
		std::cout << "You did not choose a valid option." << std::endl;

	return 0;
}


Mainmenu.h

#pragma once
#ifndef MAINMENU.h
#include <iostream>
#include <iomanip>
#include "Pizza.h"
#include "Drinks.h"
#include "Salad.h"
#include "Pasta.h"
#include "Checkout.h"
#include "logout.h"

using namespace std;

/*
-------------------------------------------------------------------
------------Main menu (This is where the program begins)-----------
-------------------------------------------------------------------
*/


class Mainmenu
{
private:
	int input, yn;
	bool done = false;

	Pizza* pizza = new Pizza;
	Drinks* drink = new Drinks;
	Salad* salad = new Salad;
	Pasta* pasta = new Pasta;
	Logout* lout = new Logout;
	Checkout check;

public:
		//Main menu constructor
	Mainmenu::Mainmenu() {};
	void menu()
	{

		float Total = 0;

		while (!done)
		{
			system("cls");

			cout << "What would you like to order?\n"
				"1.  Pizza\n"
				"2.  Pasta\n"
				"3.  Salad\n"
				"4.  Drinks\n"
				"5.  Done" << endl;
			cin >> input;

			if (input == 1)
			{
				Total += pizza->getPizza();
			}
			else if (input == 2)
			{
				Total += pasta->getPasta();
				
			}
			else if (input == 3)
			{
				Total += salad->getSalad();
			}
			else if (input == 4)
			{
				Total += drink->getDrinks();
			}
			else if (input == 5)
			{
				lout->exit();
				done = true;
			}
			else
				cout << "You did not enter a valid option" << endl;

			//Prompts user if they would like to add anything else.
			system("cls");
			cout << "Would you like to order anything else?\n"
				"1.  Yes\n"
				"2.  No" << endl;
			cin >> yn;

			if (yn == 2)
			{
				done = true;
			}

		}
		//Displays users total after tax.

		check.checkout(Total);

		system("pause");

	}
		//Main menu destructor
	Mainmenu::~Mainmenu() {};
};

#endif


Pasta.h

#pragma once
#ifndef PASTA.h
#include <iostream>

using namespace std;

/*
-------------------------------------------------------------------
---------This will prompt user to add a pasta to the order---------
-------------------------------------------------------------------
*/

class Pasta
{
private:
	int option;
	bool done = false;

public:
	//Pasta Constructor
	Pasta::Pasta() {};

float getPasta()
{
	
	system("CLS");

	float pastaTotal = 0;
	
	while (!done)
	{
		cout << "What pasta would you like to add to your order?\n"
			"1.  Chicken Parmesan\n"
			"2.  Chicken Alfredo\n"
			"3.  Chicken Marsala\n"
			"4.  Spaghetti Bowl\n"
			"5.  Veal Parmesan\n"
			"6.  Done" << endl;
		cin >> option;

		//Adds 7.99 to total
		if (option == 1)
		{
			system("cls");
			cout << "Chicken Parmesan was added to the order for $7.99.\n";
			pastaTotal += 7.99f;

		}
		//Adds 8.99 to total
		else if (option == 2)
		{
			system("cls");
			cout << "Chicken Alfredo was added to the order for $8.99.\n";
			pastaTotal += 8.99f;

		}
		//Adds 6.95 to total
		else if (option == 3)
		{
			system("cls");
			cout << "Chicken Marsala was added to the order for $6.95.\n";
			pastaTotal += 6.95f;
		}
		//Adds 5.50 to total
		else if (option == 4)
		{
			system("cls");
			cout << "Spaghetti Bowl was added to the order for $5.50.\n";
			pastaTotal += 5.50f;
		}
		//adds 10.95 to total
		else if (option == 5)
		{
			system("cls");
			cout << "Veal Parmesan was added to the order for $10.95.\n";
			pastaTotal += 10.95f;
		}
		//breaks loop
		else if (option == 6)
		{
			system("cls");
			done = true;
		}
		//not valid option
		else
			cout << "You did not chose a valid option.";

	}

	//Asks user if they want to add more to the order.  No breaks loop, yes starts loop over
	system("cls");
	cout << "Would you like to add another pasta to your order?\n"
		"1.  Yes\n"
		"2.  No" << endl;

	cin >> option;

	// breaks loop
	if (option == 2)
	{
		done = true;
	}

	// returns the total for pastas
	return pastaTotal;
}
	//Pasta Deconstructor
	Pasta::~Pasta() {};
};

#endif 


It does the same thing no matter what option you choose in the main menu.

Last edited on
It's kind of hard to read without indentation, but it looks like you have a return statement within the while loop. The return statement is going to terminate the function and return whatever value you currently have in pastaTotal.

I snipped out a bunch of code in the middle here. If you edit your post, highlight the code part, then click on the button to the right of the post under Format that looks like <>, that will put the code in code tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
float getPasta()
{
    system("CLS");

    float pastaTotal = 0;

    while (!done)
    {
        cout << "What pasta would you like to add to your order?\n"
        "1. Chicken Parmesan\n"
        "2. Chicken Alfredo\n"
        "3. Chicken Marsala\n"
        "4. Spaghetti Bowl\n"
        "5. Veal Parmesan\n"
        "6. Done" << endl;
        cin >> option;

        //....

        // returns the total for pastas
        return pastaTotal;
    }
//... 
closed account (4wvoLyTq)
Thank you for the pointer, it may be easier to read now. I do not think the return statement is within the while loop after rechecking it. Instead of executing the while loop again, it goes straight to:

cout << "Would you like to add another pasta to your order?\n"
"1. Yes\n"
"2. No" << endl;
It seems to me that lines [227, 239] need to be moved into the body of the while loop immediately above them.

However, this is a lot of code, and you've not really explained your problem very well. Which menu (I see two)? What is your input? What do you receive as output? What did you expect?

Also, C++ isn't Java --- you're abusing new. Even worse, you're leaking all sorts of memory. There isn't a single delete present in your code.

Consider:
Pizza* pizza = new Pizza;
versus
Pizza pizza;
Why should you force yourself to deal with the pain of managing memory yourself and expose all of the potential problems that arise when dealing with raw pointers? Prefer automatic storage duration when possible.
closed account (4wvoLyTq)
Thanks for the input, I have only been working with c++ for about a week and still getting down those little details. Just wanted to get the code working first and then fix up the small details.

However, when the code launches and you get to the main menu and you enter 2 for pasta you add all the items to your order, it then takes you back to the main menu if you say yes to adding another item. If you chose the same number or one you have already been into such as drinks or pizza, it takes you straight to the

cout << "Would you like to add another pasta to your order?\n"
"1. Yes\n"
"2. No" << endl;
or whatever item you chose., rather than getting into the while loop and adding more items to your order.
I think I understand what you're getting at. The while loop runs the first time the pasta menu is selected because the done variable is initialized to False. Done is set to True when the pasta menu is exited. If you then choose pasta again from the main menu, Done is still True, so the condition to run the while loop while (!done) won't be true.

The while loop is not entered, so the code jumps to these lines following the while loop. (Since they're outside the loop, entering 1. Yes here won't restart the loop. And done was already set to true when 6. Done was selected from the pasta menu to break the loop.)

1
2
3
4
5
6
7
8
9
10
11
12
13
	//Asks user if they want to add more to the order.  No breaks loop, yes starts loop over
	system("cls");
	cout << "Would you like to add another pasta to your order?\n"
		"1.  Yes\n"
		"2.  No" << endl;

	cin >> option;

	// breaks loop
	if (option == 2)
	{
		done = true;
	}


closed account (4wvoLyTq)
Exactly, so my question would be the how to do I get done to be reinitialized back to false every time the function is called?
closed account (4wvoLyTq)
Thanks for your help guys, I figured it out. I simply too the boolean assignment of done and put it into the actual function rather than making it global private in the class.
Topic archived. No new replies allowed.