dimanche 23 février 2020

How to repeat input command if user input is invalid with a string variable - C++

So I have very little coding experience and the code I wrote has this problem of asking the user to input again if correctly choosing "yes" the first time. It works correctly if the user inputs "no" or if the user writes an invalid option, the next set of questions would work. I haven't found any examples dealing with string variables without using arrays. Thanks - P.S. I know its crappy form but I'm just trying to get it to work.

using namespace std;

int main() {
  string choice;

  cout << "Do you choose to go fight in the war??\n\n";
  cout << "choose yes or no\n";

  cin >> choice;

  while (choice != "yes" || choice != "no") {
    cout << "pls enter again\n";
    cin >> choice;
    if (choice == "no") {
      cout << "you live";
      break;
    } else
      (choice == "yes");
    {
      cout << "you die";
      break;
    }
  }

Aucun commentaire:

Enregistrer un commentaire