0

I'm trying to write a simple program where I see if a user qualifies for a loan.

I'm trying to do validation to make sure the user doesn't enter gibberish, and when I do, it doesn't exit the while loop even when it's not true anymore.

The while loop doesn't work.

#include <iostream>
using namespace std;

int main() {
  string employed;

  cout << "Are you currently employed?\n";
  cin >> employed;

  while (employed != "yeah" || employed != "no" || employed != "yes" ||
         employed != "Yes") {
    cout << "Enter yes or no.";
    cin >> employed;
  }
  // ...
}

0

3 Answers 3

4

Of course you wouldn't be able to pass the while loop, all string would satisfy the while condition. I think you meant:

while (employed != "yeah" && employed != "no" && employed != "yes" && employed != "Yes")

That is, employed is none of the controlled inputs.

Sign up to request clarification or add additional context in comments.

5 Comments

Why does this work? What is the difference between || and &&? I'm really confused?
|| means or, && means and, so while (employed != "yes" || employed != "no") breaks only if employed is both "yes" and "no", while while (employed != "yes" && employed != "no") breaks if employed is neither yes nor no.
My brain is confusing double negatives / double positives? Does this mean as long as the while loop is true (the user input isn't yes or no) the while loop keeps activating? Or does it mean while the user is inputting yes/no the while loop is executing?
That's plain English, while (this is true) do whatever inside the brackets.
Also what do you mean by breaks? In sentecnce form / simple english, how would you mentally read both while conditions with || versus &&? Basically how would you understand the two differently?
4

Just change || to && in your while conditions.

while (employed != "yeah" && employed != "no" && employed != "yes" && employed != "Yes")
{
    cout << "Enter yes or no.";
    cin >> employed;
}

Comments

0

I'll try to explain what the problem is in more detail, let's simplify the while condition for a second:

while (employed != "yes" || employed != "no")

This means the loop will run as long as the inner expression evaluates to true. Suppose the user enters "yes". Here's how that will play out in steps, replacing employed with its value "yes"

1) while ("yes" != "yes" || "yes" != "no")

we can see that "yes" != "yes" is false, so let's fill that in

2) while (false || "yes" != "no")

now we evaluate the right side of the or || operator. "yes" != "no" well, yes is not equal to no, so that is true

3) while (false || true)

Since it's a logical or operator, the result will be true as long if at least one of its operands is true. We have one true and one false, so the || operator produces true as a result

4) while (true)

Thus your loop never exits, because at least one of the right or left will always be true. Instead use the and operator && which is only true if both of its operators are true:

while (employed != "yes" && employed != "no")

Which we can read as "continue as long as employed is not "yes", and employed is not "no". You can chain on as many extra words as you would like to support.

11 Comments

Your explanation is actually pretty confusing (for me, I mean). Could you explain the difference between || and &&? Conceptually what's the difference exactly?
Also, how would you read a while loop with the condition || versus && in sentence form? I knwo you explained it but I'm confused. My brain is confusing double negatives / double positives? Does this mean as long as the while loop is true (the user input isn't yes or no) the while loop keeps activating? Or does it mean while the user is inputting yes/no the while loop is executing?
@ShahJacob the not equal and can get weird yeah. It can be easier to examine it without the negatives like if (entry == "yes" || entry == "y") read out loud is "if entry is yes or entry is y." This translates pretty easily to the code.
@ShahJacob another way to set up your condition would be to check if it's in the set of things you want to check against employed == "yes" || employed == "no" etc, then invert the whole thing with a not: ` while (!(employed == "yes" || employed == "no"))`
&& "and" means both things are true, || "or" means one or both things are true. if (raining || cold) { wear_a_jacket(); } you wear a jacket if it is raining, or if it's cold, or if it's raining and cold. However if (weekend && no_early_plans) { sleep_late(); } says that you only sleep late if it is the weekend and you have no early plans.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.