C ++ While循环即使在正确答案下也可以无限循环 [英] C++ While loop in an infinite loop even on correct answer

查看:89
本文介绍了C ++ While循环即使在正确答案下也可以无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,这种非常简单的while循环保持无限循环.也许我盯着这个眼睛太久了,或者犯了一些真正明显的愚蠢错误.有人可以帮我修复代码吗?我尝试键入"ste",但它一直为我提供无效的输出.

For some reason, this very simple while loops keeps infinitely looping. Perhaps I've been staring at this too long or made some real obvious, foolish mistake. Can someone help me fix the code? I tried typing "ste" but it keeps on giving me the output of invalid.

while (type1 != "nor" || type1 != "fir" || type1 != "wat" || type1 != "ele" || type1 != "gra" || type1 != "ice" || type1 != "fig"|| type1 != "poi" || type1 != "gro" || type1 != "fly" || type1 != "psy" || type1 != "bug" || type1 != "roc" || type1 != "gho" || type1 != "dra" || type1 != "dar" || type1 != "ste" || type1 != "fai" || type1 != "n/a")
{
    cout << "Invalid type. Re-enter type." << endl;
    cin >> type1;
}

推荐答案

此循环不可能永远结束.这种外观结束的唯一可能方法是将 type1 设置为 nor fir wat ,.... 同时.您可能希望将所有ors( || )替换为ands(&& ).在这种情况下,您的循环会在 type1 等于比较的字符串之一( nor fir wat ...),当然可以.

It is impossible for this loop to ever end. The only possible way this look could end would be for type1 to be nor, fir, wat, ... at the same time. You probably want to replace all ors (||), by ands (&&). In that case your loop would stop at the moment type1 is equal to one of the compared strings (nor, fir, wat... ), which of course is possible.

这篇关于C ++ While循环即使在正确答案下也可以无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆