while loop does not work

Ok, so I have the basics of a program that has a while loop. Here is the code:

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
// Fortune Teller
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
   //first choice
   string choice1;
   cout << "Do you like chicken nuggets: ";
   cin >> choice1;
   if (choice1 == "Yes" || choice1 == "yes")
   {}
   else
   {
       while (choice1 != "Yes" || choice1 != "yes" || choice1 != "No" || choice1 != "no");
       {
           cout << "Sorry, your answer did not seem to qualify. You must answer Yes or No. Please try again" << endl;
           cout << "Do you like chicken nuggets: ";
           cin >> choice1;
       }
   }
   return 0;
}


Everything works except the loop. When i type (for example) "lakjhf", the loop does not activate. When i take out the loop, the program works perfectly except the fact that there is no loop.
You have a semicolon at the end of line 18 that doesn't belong there.
And the while condition will always be true, as choice1 can't be equal to all of those at the same time.
@ Athar "And the while condition will always be true, as choice1 can't be equal to all of those at the same time."

The condition is finding if it not true (!=), not finding if it is true (==)

EDIT: Nevermind, you were right. I had to switch the "!=" into "==". Thanks!
Last edited on
Look at your condition carefully. It will be true of choice1 isn't "Yes" OR if it isn't "yes". Since it can't be both, it follows that it will have to be NOT one of those, making at least one of them true and thus the whole statement true.
while (choice1 != "Yes" && choice1 != "yes" && choice1 != "No" && choice1 != "no")
replace line 18 with this and it should work
Dapasta! You know that "-----" is use to print any thing on screen and '------' to equalize any value to any variable.You write YES and NO in "" ,instead of it you should write them between ' ' . You should write that in this way
Line where you give condition with if:
if (choice1 == 'Yes' || choice1 == 'yes')
Line wher you give condition of while loop:
while (choice1 != 'Yes' || choice1 != 'yes' || choice1 != 'No' || choice1 != 'no');
Topic archived. No new replies allowed.