开发者

Allowing multiple types of input for various forms of "yes" and "no" in C++

I posted a question about how to get user input such as YES or NO to control the flow of a program using if else statements, I got a answer and now i'm a step closer to making 开发者_运维问答this work, however another problem has arisen, i really need to allow for multiple inputs, for example this is what im trying:

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;
}

Kiril Kirov posted this code that could help:

if( std::string::npos != input.find( "no" ) )

but i couldn't get it to work, and roger pate suggested this:

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

however i never tried this as its complexity is far beyond my understanding. i was hoping for a solution a beginner programmer could understand or maybe im just a really slow learner


EDIT: I made this modification but it still doesn't work any better then before, if i give the wrong case it goes to else (error) and there is no where to add more words, (such as NO N no No) :

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;
}


Add the headers

#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" ; 
}


In most languages the easy way is to uppercase the string before comparing it.

In standard C++ uppercasing is, unfortunately, more complex. It has to with very heavy resistance to having any new feature that doesn't work perfectly in every conceivable case. And uppercasing is a feature that by it's nature -- different in different countries, sometimes even context-sensitive -- cannot work perfectly in every conceivable case.

Adding to that, the C library's uppercase function is a bit difficult to use correctly.

Dang, I'd give you a reasonable uppercase function right here but no time. :-( Search for earlier questions on uppercasing. That should work! :-)

Cheers,


The simpler way is to convert the case beforehand. Assuming that the user restricts its input to one of the valid strings (yes/no).

Check out Boost.String, it's a collection of algorithms on the std::string class (and specifically here case conversions routines).

It'll work great for ASCII characters, but since we're talking of std::string that should be okay, you're not planning on handling japanese or arabic are you :) ?


How about you just check first two characters of the string and see if they are n N or Y y?

I haven't used C++ strings in a while, but there are several functions which look interesting. Take a look at this site. You could for example take length of a string. Then you could take characters at positions zero, one and if available two using functions I liked to. After that see if the first character is Y,y, N, n. You could go on if you want to be even more sure that user hasn't inputted nonsense (if first letter is N or n check if second in O or o and so on), but I think that this should be enough for simple decision.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜