Using user input, such as YES and NO, to control program flow in C ++

I am creating a small program that uses the if else statement, but instead of using numbers to control the flow, I want the control to work with yes and no,

eg:

cout << "would you like to continue?" << endl;
cout << "\nYES or NO" << endl;
int input =0;
cin >> input;
string Yes = "YES";
string No = "NO";

if (input == no)
{
    cout << "testone" << endl;
}
if (input == yes)
{
    cout << "test two" << endl;
         //the rest of the program goes here i guess?
}
else
{
    cout <<  "you entered the wrong thing, start again" << endl;
              //maybe some type of loop structure to go back
}

but I don't seem to be able to use any variations of this, I can get the user to enter 0 or 1 instead, but it seems really dumb, I would prefer it to be as natural as possible, users don’t tell if they ?

also i just need to add more words, for example "no NO No noo no n", all of this would mean no

hope that makes sense

, ++ , - Windows.

+1
4

string, int.

:

string input;

int input = 0;

, ++ , Yes, Yes. .

btw, if else if, , "", else.

+4

, input std::string, int.

, yes no :

             v
if (input == No)
// ..
//                v
else if (input == Yes)
^^^^

, "no no no..", std::string::find:

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

"".

, - ( , ), find. , yes - .

+2
string input;
cin >> input;
if (input == "yes"){

}
else if (input == "no"{

}

else {
    //blah
}
0
bool yesno(char const* prompt, bool default_yes=true) {
  using namespace std;
  if (prompt && cin.tie()) {
    *cin.tie() << prompt << (default_yes ? " [Yn] " : " [yN] ");
  }
  string line;
  if (!getline(cin, line)) {
    throw std::runtime_error("yesno: unexpected input error");
  }
  else if (line.size() == 0) {
    return default_yes;
  }
  else {
    return line[0] == 'Y' || line[0] == 'y';
  }
}
0

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


All Articles