在 c++e 中使用 do-while 循环进行密码检查时出现错误 "code will never execute"
Getting error "code will never execute" when using do-while loop for password checking function in c++e
我在我的密码函数中加入了一个 do/while 循环,但它不起作用。我使用 xcode10 编写 C++ 代码,当我在 while 语句后使用分号时,它会显示一条错误消息,指出代码永远不会执行
string password (string g)
{
string ch = "hello" ;
cout << "Enter password";
getline(cin, g);
do {
if (ch.compare(g) != 0) {
cout << " INCORRECT PASSWORD";
return (0);
} else {
cout << "correct password";
return (string(g));
}
} while (ch.compare(g) == 0); //this is where it shows the error that the code will never exec
}
我想加入这个循环和其他一些东西,这样我就可以让它成为一个无限循环,直到您输入正确的密码。
好吧,在你的 if
语句中,你将 return 在这两种情况下导致函数停止,因此它永远不会到达 while 条件来测试它
string password(string g)
{
string ch = "hello";
cout << "Enter password\n";
do
{
getline(cin, g);
if (ch.compare(g) != 0)
{
cout << " INCORRECT PASSWORD\n";
}
else {
cout << "correct password";
return (string(g));
}
} while (ch.compare(g) != 0);
}
在“if
”和“else
”中都有Return语句。
您可以看到,无论 ch.compare(g)
的结果是什么,该函数都会 return 给它的调用者。
这就是它永远不会执行“while
”的原因。
尝试在代码的不同位置设置 Return 语句 :)
如果 EOF
.
,您还需要检查是否也收到了输入
string password() { // you don't need g as parameters in, your overwriting it
string const ch = "hello"; // make ch const to show it not supposed to change
cout << "Enter password";
string g; // defining g here since now you need it
while (getline(cin, g)) { // check that the read from cin is OK
if (ch != g) { // simple notation for comparing two strings. There's also == available
cout << "INCORRECT PASSWORD. Please try again\n"; // no return if you want to try again
} else {
cout << "correct password";
return g; // you could also return ch here since they have the same content
}
}
cout << "Unable to read line. aborting\n"; // not really a lot you can do if there is no input to read.
return string(); // returning empty string.
}
我在我的密码函数中加入了一个 do/while 循环,但它不起作用。我使用 xcode10 编写 C++ 代码,当我在 while 语句后使用分号时,它会显示一条错误消息,指出代码永远不会执行
string password (string g)
{
string ch = "hello" ;
cout << "Enter password";
getline(cin, g);
do {
if (ch.compare(g) != 0) {
cout << " INCORRECT PASSWORD";
return (0);
} else {
cout << "correct password";
return (string(g));
}
} while (ch.compare(g) == 0); //this is where it shows the error that the code will never exec
}
我想加入这个循环和其他一些东西,这样我就可以让它成为一个无限循环,直到您输入正确的密码。
好吧,在你的 if
语句中,你将 return 在这两种情况下导致函数停止,因此它永远不会到达 while 条件来测试它
string password(string g)
{
string ch = "hello";
cout << "Enter password\n";
do
{
getline(cin, g);
if (ch.compare(g) != 0)
{
cout << " INCORRECT PASSWORD\n";
}
else {
cout << "correct password";
return (string(g));
}
} while (ch.compare(g) != 0);
}
在“if
”和“else
”中都有Return语句。
您可以看到,无论 ch.compare(g)
的结果是什么,该函数都会 return 给它的调用者。
这就是它永远不会执行“while
”的原因。
尝试在代码的不同位置设置 Return 语句 :)
如果 EOF
.
string password() { // you don't need g as parameters in, your overwriting it
string const ch = "hello"; // make ch const to show it not supposed to change
cout << "Enter password";
string g; // defining g here since now you need it
while (getline(cin, g)) { // check that the read from cin is OK
if (ch != g) { // simple notation for comparing two strings. There's also == available
cout << "INCORRECT PASSWORD. Please try again\n"; // no return if you want to try again
} else {
cout << "correct password";
return g; // you could also return ch here since they have the same content
}
}
cout << "Unable to read line. aborting\n"; // not really a lot you can do if there is no input to read.
return string(); // returning empty string.
}