C++ 允许对各种形式的“输入”进行多种类型的输入;是”;及;否";在C++;

C++ 允许对各种形式的“输入”进行多种类型的输入;是”;及;否";在C++;,c++,if-statement,control-flow,C++,If Statement,Control Flow,关于如何使用if-else语句获取用户输入(如是或否)以控制程序流,我得到了答案,现在我离实现这一点又近了一步,但是出现了另一个问题,我确实需要考虑多个输入,例如,这就是我正在尝试的: if (input == ("YES" || "yes" || "y" || "Yes" || "Y")) { cout << "you said yes" << endl; } else if (input == "NO", "no", "n", "No","N") {

关于如何使用if-else语句获取用户输入(如是或否)以控制程序流,我得到了答案,现在我离实现这一点又近了一步,但是出现了另一个问题,我确实需要考虑多个输入,例如,这就是我正在尝试的:

if (input == ("YES" || "yes" || "y" || "Yes" || "Y"))
{
    cout << "you said yes" << endl;
}
else if (input == "NO", "no", "n", "No","N")
{
    cout << "you said no" << endl;
}
else 
{
    cout <<  "ERROR!!!" << endl;
}
但我无法让它发挥作用,罗杰·佩特建议:

if (prompt && cin.tie()) {
*cin.tie() << prompt << (default_yes ? " [Yn] " : " [yN] ");
if(提示符和&cin.tie()){

*cin.tie()在大多数语言中,最简单的方法是在比较字符串之前将其大写

在标准C++中,不幸的是,更复杂。它必须具有非常强的抵抗力,使其在任何可能的情况下都不能完美地工作。而UpHeCube是一个特性,它的性质不同国家,有时甚至是上下文敏感的,在每一个可能的CA中都不能完美地工作。嗯

此外,C库的大写函数有点难以正确使用

Dang,我会在这里给你一个合理的大写函数,但没有时间。:-(搜索前面关于大写的问题。应该有用!)


干杯,

更简单的方法是事先转换大小写。假设用户将其输入限制为一个有效字符串(是/否)

检查一下,这是
std::string
类上的算法集合(这里特别是case转换例程)


这对ASCII字符很有用,但既然我们谈论的是
std::string
,那应该没问题,你就不打算处理日语或阿拉伯语了,是吗:)?

检查字符串的前两个字符,看看它们是n还是Y

我以前还没有使用C++字符串,但是有几个看起来很有趣的函数。看看站点。你可以举例说一个字符串。然后你在0位的字符,一个,如果有两个函数,我喜欢。然后看看第一个字符是Y,Y,N,N。如果你想成为Mo,你可以继续。请确保用户没有输入无意义的内容(如果第一个字母是N或N,请检查第二个字母是O还是O等等),但我认为这对于简单的决定应该足够了。

添加标题

#include <algorithm>
#include <cctype>

cout << "\nYES or NO" << endl; 
string input =""; 
cin >> input; 
transform (input.begin(), input.end(), input.begin(),tolower);

if ( (std::string::npos != input.find( "yes" )) || (std::string::npos != input.find( "y" )) ) 
{
     cout << "you said yes \n" ; 
}
else if ( (std::string::npos != input.find( "no" ) )  || (std::string::npos != input.find( "n" ) ) )
{
    cout << "you said no \n" ; 
}
else  
{
    cout <<  "ERROR!!! \n" ; 
}
#包括
#包括
不能输入;
转换(input.begin()、input.end()、input.begin()、tolower);
if((std::string::npos!=input.find(“yes”))| |(std::string::npos!=input.find(“y”))
{

你不能开始工作吗?“我不能让它工作”没有告诉我们很多——你能说得更具体吗?+ 1,因为有人没有理性地使用ToupP/ToWar进行下投票,然后转换成上/小写,然后检查。这会减少你的比较。”阿尔夫:我认为保罗说的是一个很好的理由。(虽然我没有自己投票,也不知道保罗有没有。)OPS问题可以很容易地使用String来实现。为这项工作添加另一个库将是多余的。@哑铃编码器:我认为Boost(模板部分)与标准库一样可用。我同意这很简单,但实际上字符串算法库是值得使用的,我刚刚看到太多蹩脚的字符串例程。
cout << "\nYES or NO" << endl;
string input ="";
cin >> input;

if ( std::string::npos != input.find( "yes" ) )
{
    cout << "you said yes" << endl;
}
else if ( std::string::npos != input.find( "no" ) )
{
    cout << "you said no" << endl;
}
else 
{
    cout <<  "ERROR!!!" << endl;
}
#include <algorithm>
#include <cctype>

cout << "\nYES or NO" << endl; 
string input =""; 
cin >> input; 
transform (input.begin(), input.end(), input.begin(),tolower);

if ( (std::string::npos != input.find( "yes" )) || (std::string::npos != input.find( "y" )) ) 
{
     cout << "you said yes \n" ; 
}
else if ( (std::string::npos != input.find( "no" ) )  || (std::string::npos != input.find( "n" ) ) )
{
    cout << "you said no \n" ; 
}
else  
{
    cout <<  "ERROR!!! \n" ; 
}