简体   繁体   English

在C ++中测试作业中的字符串输入时,无法退出while循环(作为数据验证)

[英]Unable to exit while loop (as data validation) while testing for string input in homework problem in C++

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. 我正在尝试进行验证,以确保用户不会输入乱码,而当我输入时,即使不再正确,也不会退出while循环。

The while loop doesn't work. while循环不起作用。

#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;
  }
  // ...
}

Of course you wouldn't be able to pass the while loop, all string would satisfy the while condition. 当然,您将无法传递while循环,所有字符串都将满足while条件。 I think you meant: 我想你的意思是:

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

That is, employed is none of the controlled inputs. 即, employed的不是受控输入。

Just change || 只需更改|| to && in your while conditions. &&while条件。

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

I'll try to explain what the problem is in more detail, let's simplify the while condition for a second: 我将尝试更详细地解释问题所在,让我们先简化一下while条件:

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

This means the loop will run as long as the inner expression evaluates to true. 这意味着只要内部表达式的计算结果为true,循环就会运行。 Suppose the user enters "yes" . 假设用户输入"yes" Here's how that will play out in steps, replacing employed with its value "yes" 以下是如何将逐步发挥出来,代替employed它的值"yes"

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

we can see that "yes" != "yes" is false , so let's fill that in 我们可以看到"yes" != "yes"false ,所以我们将其填写

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

now we evaluate the right side of the or || 现在我们评估or ||的右侧 operator. 操作员。 "yes" != "no" well, yes is not equal to no, so that is true "yes" != "no"好,yes等于no,所以这是真的

3) while (false || 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. 由于它是逻辑运算符,因此只要至少一个操作数为true,结果就为true。 We have one true and one false, so the || 我们有一个真和一个假,所以|| operator produces true as a result 运算符结果为true

4) while (true) 4) while (true)

Thus your loop never exits, because at least one of the right or left will always be true. 因此,您的循环永远不会退出,因为左右至少之一始终为true。 Instead use the and operator && which is only true if both of its operators are true: 而是使用and运算符&& ,仅当两个运算符均为true时,才为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. 只要被雇用不是“是”,我们就可以理解为“继续”,被雇用不是“否”,您可以根据需要添加更多的单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM