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

I posted a question about how to get user input, such as YES or NO, to control the flow of the program using if else instructions, I got an answer and now I’m one step closer to doing this work, however there was another problem, I really need to resolve some input, for example, is what I'm 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;
}

Kirill Kirov posted this code that could help:

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

but I could not get it to work, and Mr. Roger suggested the following:

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

however, I have never tried this, since its complexity far exceeds my understanding. I was hoping for a solution that a novice programmer could understand, or maybe just a very slow student


EDIT: , , , (), , (, 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;
}
+3
4

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

.

++ , , . - , . - , - , -, - .

, C .

, , .:-( . !: -)

,

0

- . , (/).

Boost.String, std::string (, , case).

ASCII, std::string, , , :)?

0

, , n N Y y?

++ , , . . , length . , , . , Y, y, N, n. , , ( N n , O o ..), , .

0

Source: https://habr.com/ru/post/1773238/


All Articles