为什么我的while循环被跳过了?

RID*_*ron 0 c++ while-loop

程序跳过我的while循环并结束.超级沮丧.我甚至在while循环之前将AnsCheck的值设置为false.没运气.该程序不执行While循环中的任何操作.这是相关的代码:

bool AnsCheck;
AnsCheck = false;
while (AnsCheck = false)
{
    getline(cin, Ans1);
    if (Ans1 != "T" || Ans1 != "F")
    {
        cout << "Please Enter T for true or F for False" << endl;
        cout << "answer not T or not F" << endl; // debugging
    }
    else
    {
        AnsCheck = true;
        cout << "changed bool to true" << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

ndm*_*iri 6

您需要使用比较运算符来表示相等==而不是赋值运算符=.

while (AnsCheck == false) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

此外,正如您在此答案下面的评论中提到的,if语句中的条件永远不会被评估为true.要比较字符串,您应该使用strcmp,当两个c字符串的内容相等时返回0.有关更多信息,请参阅此参考.

if (strcmp(Ans1, "T") != 0 && strcmp(Ans1, "F") != 0) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)