我的程序一直在循环,永远不会到达 "return 0;"。是编译器不好还是代码不好?
My program keeps looping and never get's to "return 0;". Is it the compiler that's bad or the code?
我的程序一直在循环,永远不会到达 "return 0;"。是编译器不好还是代码不好?
#include<iostream>
using namespace std;
int main() {
string nameInput = "";
string Input = "Yes";
cout << "Welcome to the purple casino!" << endl << "What's your name?" << endl;
while(Input =="Yes" || "yes"){
getline(cin, nameInput);
cout << nameInput << ", what a nice name!" << endl << "Do you want to change it?" << endl;
getline(cin, Input);
if(Input =="Yes" || "yes"){
cout << "To what?" << endl;
}
}
cout << "Let's begin!";
return 0;
}
My program keeps looping and never get's to “return 0;”. Is it the compiler that's bad or the code?
代码,(几乎)一如既往。
Input =="Yes" || "yes"
将始终评估为 true
,无论 Input
的实际值是多少,因为它归结为:
true
如果:Input
等于 "Yes" 或 "yes".
false
否则。
字符串文字的计算结果为 true
,因此逻辑或的第二个操作数将为 true
,导致整个逻辑表达式的计算结果为 true
,始终!
因此,您的 while 循环的条件始终为真,导致无限循环!
所以改变这个:
while(Input =="Yes" || "yes")
对此:
while(Input =="Yes" || Input == "yes")
PS: 类似地更改 if 语句的条件,因为它是完全相同的条件。
您的 while
语句条件错误:
while (Input == "Yes" || "yes")
as "yes"
操作数的计算结果始终为 true
,导致整个条件为 true
。试试这个:
while (Input == "Yes" || Input == "yes")
表达式 Input == "Yes" || "yes"
被求值,由于 运算符优先级 ,如
(Input == "Yes") || "yes"
始终是 true
。这是因为 const char[4]
文字 "yes"
衰减 为 const char*
类型,具有非零指针值。
你需要Input == "Yes" || Input == "yes"
您的代码中有一个愚蠢的错误。请查看下面编辑的代码 -
#include<iostream>
using namespace std;
int main(){
string nameInput = "";
string Input = "Yes";
cout << "Welcome to the purple casino!" << endl << "What's your name?" <<
endl;
while(Input =="Yes" || Input == "yes"){ // Error was here
getline(cin, nameInput);
cout << nameInput << ", what a nice name!" << endl << "Do you want to
change it?" << endl;
getline(cin, Input);
if(Input =="Yes" || "yes"){
cout << "To what?" << endl;
}
}
<< "Let's begin!";
return 0;
}
尝试将 while(Input =="Yes" || "yes"){
修改为 while(Input =="Yes" || Input == "yes"){
我认为问题会得到解决
嗨,在 return 尝试之前:
cout << "Let's begin!";
并将 While 和 if 条件更改为:
(Input =="Yes" || Input=="yes")
以更一般的方式,如果您有一个循环,您的程序永远不会 returns,这意味着您传递的条件始终计算为 true
。
在你的情况下,正如其他人已经回答的那样,条件
while (Input == "Yes" || "yes")
由于第二部分,计算结果总是为真。
您真正想要的是检查输入是否为 "Yes" 或输入是否为 "yes",在 C++ 中应写为:
while (Input == "Yes" || Input == "yes").
希望这个更笼统的回答能有所帮助。
我的程序一直在循环,永远不会到达 "return 0;"。是编译器不好还是代码不好?
#include<iostream>
using namespace std;
int main() {
string nameInput = "";
string Input = "Yes";
cout << "Welcome to the purple casino!" << endl << "What's your name?" << endl;
while(Input =="Yes" || "yes"){
getline(cin, nameInput);
cout << nameInput << ", what a nice name!" << endl << "Do you want to change it?" << endl;
getline(cin, Input);
if(Input =="Yes" || "yes"){
cout << "To what?" << endl;
}
}
cout << "Let's begin!";
return 0;
}
My program keeps looping and never get's to “return 0;”. Is it the compiler that's bad or the code?
代码,(几乎)一如既往。
Input =="Yes" || "yes"
将始终评估为 true
,无论 Input
的实际值是多少,因为它归结为:
true
如果:Input
等于 "Yes" 或 "yes".false
否则。
字符串文字的计算结果为 true
,因此逻辑或的第二个操作数将为 true
,导致整个逻辑表达式的计算结果为 true
,始终!
因此,您的 while 循环的条件始终为真,导致无限循环!
所以改变这个:
while(Input =="Yes" || "yes")
对此:
while(Input =="Yes" || Input == "yes")
PS: 类似地更改 if 语句的条件,因为它是完全相同的条件。
您的 while
语句条件错误:
while (Input == "Yes" || "yes")
as "yes"
操作数的计算结果始终为 true
,导致整个条件为 true
。试试这个:
while (Input == "Yes" || Input == "yes")
表达式 Input == "Yes" || "yes"
被求值,由于 运算符优先级 ,如
(Input == "Yes") || "yes"
始终是 true
。这是因为 const char[4]
文字 "yes"
衰减 为 const char*
类型,具有非零指针值。
你需要Input == "Yes" || Input == "yes"
您的代码中有一个愚蠢的错误。请查看下面编辑的代码 -
#include<iostream>
using namespace std;
int main(){
string nameInput = "";
string Input = "Yes";
cout << "Welcome to the purple casino!" << endl << "What's your name?" <<
endl;
while(Input =="Yes" || Input == "yes"){ // Error was here
getline(cin, nameInput);
cout << nameInput << ", what a nice name!" << endl << "Do you want to
change it?" << endl;
getline(cin, Input);
if(Input =="Yes" || "yes"){
cout << "To what?" << endl;
}
}
<< "Let's begin!";
return 0;
}
尝试将 while(Input =="Yes" || "yes"){
修改为 while(Input =="Yes" || Input == "yes"){
我认为问题会得到解决
嗨,在 return 尝试之前:
cout << "Let's begin!";
并将 While 和 if 条件更改为:
(Input =="Yes" || Input=="yes")
以更一般的方式,如果您有一个循环,您的程序永远不会 returns,这意味着您传递的条件始终计算为 true
。
在你的情况下,正如其他人已经回答的那样,条件
while (Input == "Yes" || "yes")
由于第二部分,计算结果总是为真。 您真正想要的是检查输入是否为 "Yes" 或输入是否为 "yes",在 C++ 中应写为:
while (Input == "Yes" || Input == "yes").
希望这个更笼统的回答能有所帮助。