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
#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();
}
elseif (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"
usingnamespace 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();
}
elseif (input == 2)
{
Total += pasta->getPasta();
}
elseif (input == 3)
{
Total += salad->getSalad();
}
elseif (input == 4)
{
Total += drink->getDrinks();
}
elseif (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>
usingnamespace 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
elseif (option == 2)
{
system("cls");
cout << "Chicken Alfredo was added to the order for $8.99.\n";
pastaTotal += 8.99f;
}
//Adds 6.95 to total
elseif (option == 3)
{
system("cls");
cout << "Chicken Marsala was added to the order for $6.95.\n";
pastaTotal += 6.95f;
}
//Adds 5.50 to total
elseif (option == 4)
{
system("cls");
cout << "Spaghetti Bowl was added to the order for $5.50.\n";
pastaTotal += 5.50f;
}
//adds 10.95 to total
elseif (option == 5)
{
system("cls");
cout << "Veal Parmesan was added to the order for $10.95.\n";
pastaTotal += 10.95f;
}
//breaks loop
elseif (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.
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.
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;
}
//...
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.
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;
}
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.