如果用户每次说“是”的话,我正试着制作一个会一遍又一遍运行的程序。不幸的是,当我输入“是”或“否”时,它似乎没有意识到,并且总是默认的“再次出现”吗?消息。这是我用来从控制台获取输入的代码:
bool getYN(){
bool confirmed = 0;
bool answer = 0;
string input;
while(!confirmed){
getline(cin, input, '\n');
transform(input.begin(), input.end(), input.begin(), toupper);
if(input.c_str() == "Y" || input.c_str() == "YES"){ //If the user says yes
confirmed = 1;
answer = 1;
} else if(input.c_str() == "N" || input.c_str() == "NO"){ //If the user says no
confirmed = 1;
answer = 0;
} else { //If the user says something else entirely
printf("\nCome again? (Y/N) ");
};
};
return answer;
};
我包括了<string>
和<algorithm>
。出于某种原因,当我输入y/yes或n/no时,它总是表现得好像没有得到y/yes或n/no。只是一直要求我再回答一次。
发布于 2016-03-21 08:42:59
if(input.c_str() == "Y" || input.c_str() == "YES"){ //If the user says yes
confirmed = 1;
answer = 1;
} else if(input.c_str() == "N" || input.c_str() == "NO"){ //If the user says no
confirmed = 1;
answer = 0;
}
你不应该像这样做c-字符串比较。您正在获取字符的地址,并与文本分配对象的地址进行比较。当然,比较将返回false。
对于c++字符串,简单的operator==
比较是有效的:
if(input == "Y" || input == "YES"){ //If the user says yes
confirmed = 1;
answer = 1;
} else if(input == "N" || input == "NO"){ //If the user says no
confirmed = 1;
answer = 0;
}
发布于 2016-03-21 09:07:07
#include <iostream>
#include <string>
using namespace std; // For speed
int main()
{
bool saidYes = false;
string input;
while (!saidYes)
{
cout << "Input yes or no: ";
getline(cin, input);
if (input == "no" || input == "n" || input == "NO")
{
saidYes = true; // breaks the loop
}
if (input == "y" || input == "Y" || input == "yes" || input == "YES")
{
saidYes = false;
}
}
return 0;
}
您可以使用上面的示例来消除大量不必要的代码,我选择不添加use语句,但是如果您在这里添加该语句,它也会工作。
您还可以进一步压缩这段代码,但这只是一个简单的示例,说明如何更好地为您工作!
如前所述,您可以使用==来比较字符串,如果您来自某些其他语言,那么习惯lol可能是一个恼人的变化。
我包括了字符串和算法。出于某种原因,当我输入y/yes或n/no时,它总是表现得好像没有得到y/yes或n/no。只是一直要求我再回答一次。
算法对于你想要做的事情并不是必需的,而且你使得字符串输入的读取和接受比它所需要的要困难得多。
如果您在上面查看,您将看到string input;
,这将是您可以用来存储用户输入字符串的变量。
您还会注意到getline(cin, input);
,这是您可以用来“读取”用户在被提示时输入的字符串。
@我最初的回答只是针对你的问题,下面这个例子是针对你对我的评论!
因此,您有很多选项,但是假设您希望用户输入是或否,并且取决于输入,同时确保用户一次又一次地被提示输入是或否,那么您所要做的就是像这样修改我原来的答案。
#include <iostream>
#include <string>
using namespace std; // For speed
int main()
{
bool saidYes = false;
string input;
while (!saidYes)
{
cout << "Input yes or no: ";
getline(cin, input);
if (input == "no" || input == "n" || input == "NO")
{
saidYes = true;
cout << "you said no" << endl;
/* breaks the loop by changing the
bool (true or false flag) to true, if you want to produce a specific result,
whether it's a simple output statement or a function call you can put it here
*/
}
else if (input == "y" || input == "Y" || input == "yes" || input == "YES")
{
saidYes = true;
cout << "You said yes" << endl;
/* breaks the loop by changing the
bool (true or false flag) to true, if you want to produce a specific result,
whether it's a simple output statement or a function call you can put it here
*/
}
else saidYes = false;
}
return 0;
}
发布于 2016-03-29 19:38:47
我已经根据当前最好的答案修改了我的代码,但我也优化了它,这样就不再需要confirmed
了。
bool getYN(){
bool answer = 0;
string input;
while(!answer){
getline(cin, input, '\n');
transform(input.begin(), input.end(), input.begin(), toupper);
if(input == "Y" || input == "YES"){
answer = 2;
} else if(input == "N" || input == "NO"){
answer = 1;
} else {
printf("\nCome again? (Y/N) ");
};
};
return answer - 1;
};
小的优化,当然,但是每一点都很重要。
https://stackoverflow.com/questions/36136823
复制