Food menu problem (problem with if statement or while loop)

My food menu program was designed to ask what the user wants to eat out of the 3 choices (burger, fries, pizza) and if the string wasn't one of the 3 options it would ask for the user to "pick something on the menu" which this part of the code works but once i enter one of the choices it will still assume that it isn't correct so if someone call tell me what part of the code needs removal, editing or adding to the code please tell me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include <string>
using namespace std;


int main ()
{
cout << "Hello, welcome to Fridays\nWe have many options to eat fromlike\n1.Burgers\n2.Fries\n3.Pizza\n";
    cout << "What would you like to eat\n";
   string food;
    cin >> food;
    while (food != "burger" || "fries" || "pizza") {
        cout << "please choose something that is on the menu\n";
        cin >> food;}
    
    
        if (food == "burger" || "fries" || "pizza") {
            
            cout << "thanks for choosing " << food << "\n";}
        
    
}
closed account (Dy7SLyTq)
i bet if you enter burger it works fine ;). || means you have to write a new condition ie after the || the food != has nothing to do with it anymore
You are way better of using a switch statement with cases and having them enter a number on the menu for each rather than type the name. For the sake of editing your code though I would start with.

(food != "burger" || "fries" || "pizza")

to

(food != "burger") || (food != "fries") || (food != "pizza")

same with the if statement, make sure you include the variable in each of the or statements.
Last edited on
closed account (Dy7SLyTq)
your code wont compile agg. you forgot 2 closing )'s.
yeh ill edit it fast , just copy and pasted quick ;)
thx @aggsyb your code fixed most of it but the problem now is when I enter the correct item the cout"thanks for choosing ____" will appear but the question "please choose something that is on the menu" will appear after it also so the program doesn't stop running after the correct item is put in
closed account (Dy7SLyTq)
will you post your code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;


int main ()
{
cout << "Hello, welcome to Fridays\nWe have many options to eat fromlike\n1.Burgers\n2.Fries\n3.Pizza\n";
    cout << "What would you like to eat\n";
    string food;
    cin >> food;
    while ((food != "burger") && (food != "fries") && (food != "pizza"))
        {
            cout << "please choose something that is on the menu\n";
            cin >> food;
        }

        //if (food == "burger" || "fries" || "pizza") {

            cout << "thanks for choosing " << food << "\n";


}
closed account (Dy7SLyTq)
you need to make the &&'s ||'s. no one said to use &&'s
In while you need && :)
closed account (Dy7SLyTq)
In while you need && :)

uhhh no your while loop needs ||'s. try running it and see what happens. youll get into an infinite loop. food cant equal three things at once, which is what it needs
Last edited on
I changed the program slightly and now I'm having problems such as when i enter burger on the first attempt the cout for picking correctly wont show up but the program will end and if i type in fries or pizza it will say it's not on the menu

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


int main ()
{
cout << "Hello, welcome to Fridays\nWe have many options to eat fromlike\n1.Burgers\n2.Fries\n3.Pizza\n";
    cout << "What would you like to eat\n";
   string food;
    
    cin >> food;
   
    while (food != "burger" && "fries" && "pizza") {
        cout << "Please choose only food that is on the menu\n";
       
        cin >>food;
       
        if (food == "burger" && "fries" && "pizza") {
            cout << "Thanks for choosing " << food << "\n";
        
    }
    }

}
closed account (Dy7SLyTq)
1) NO &&'s
2) THATS NOT HOW YOU WRITE CONDITIONS
DTS my program runs perfect
closed account (Dy7SLyTq)
sorry my bad. your right i was doing my truth tables wrong
we are only humans lol ;)
closed account (Dy7SLyTq)
http://www.cplusplus.com/forum/beginner/107837/2/#msg585440

pffft my code would run fine ;)
Thanks DTS and Chris I finally got the program to work with your help :)
you send the money to my account 192837465 lol ;)
while (food != "burger" && "fries" && "pizza")

This does not mean what you think it means, please refer to Chriscpp's code. With the way it is now, it is the same as writting:

while(food != "burger")

"fries" and "pizza" will always be true. Think of it in the computer's perspective. Say aloud to yourself the word "apple", does it mean anything?

When you input "burger", the reason that this line

cout << "Thanks for choosing " << food << "\n";

doesn't run is because you never entered the while loop in the first place. You input burger, the condition for the while loop evaluates are false and it never gets entered.

Incidentally, this will not do what you want it to do either.

1
2
 if (food == "burger" && "fries" && "pizza") {
            cout << "Thanks for choosing " << food << "\n";


I'm going to assume you're going to follow Chris's example and compare "fries" and "pizza" with food. food can never be "burger", "fries" and "pizza" at the same time, so you might want || rather than &&.

On a final note, you might want to study how Chris placed the curly braces in his code more closely.
Topic archived. No new replies allowed.