Home > Mobile >  Why does my program terminate when it should continue?
Why does my program terminate when it should continue?

Time:10-01

#include <iostream>
#include <ctime>

using namespace std;

int main() {

    int answer;
    string question;
    string play = "";
    srand(time(NULL));

    cout << "What question would you like to ask the Magic 8 ball?: \n";
    cout << "\n";
    cin >> question;
  
    answer = rand () % 8   1;

    if (answer == 1) {
      cout << "The answer is: It is certain\n";
    } else if (answer == 2) {
      cout << "The answer is: It is decidely so\n";
    } else if (answer == 3) {
      cout << "The answer is: Most likely\n";
    } else if (answer == 4) {
      cout << "The answer is: Signs point to yes\n";
    } else if (answer == 5) {
      cout << "The answer is: Ask again later\n";
    } else if (answer == 6) {
      cout << "The answer is: Don't count on it\n";
    } else if (answer == 7) {
      cout << "The answer is: My sources say no\n";
    } else {
      cout << "The answer is: Reply hazy, try again\n";
    }

    cout << "Would you like to play again?(y/n): ";
    cin >> play;

    if (play == "yes" || play == "y") {
      cin >> question;
    } else if (play == "no" || play == "n") {
      cout << "Thank you for playing with the Magic 8 Ball";
    }

    return 0;
}

It stops the program after it gives my answer, not letting the user answer if they want to play again or not. Please help me, I've been stuck on this for a while now and don't know what to do.

CodePudding user response:

you did most of the work you just have to make a loob like so

#include <iostream>
#include <ctime>

using namespace std;

int main() {

    int answer;
    string question;
    string play = "";
    srand(time(NULL));

    while(play!="no"&&play!="n")
   {
    play = "";

    cout << "What question would you like to ask the Magic 8 ball?: \n";
    cout << "\n";
    cin >> question;
  
    answer = rand () % 8   1;

    if (answer == 1) {
      cout << "The answer is: It is certain\n";
    } else if (answer == 2) {
      cout << "The answer is: It is decidely so\n";
    } else if (answer == 3) {
      cout << "The answer is: Most likely\n";
    } else if (answer == 4) {
      cout << "The answer is: Signs point to yes\n";
    } else if (answer == 5) {
      cout << "The answer is: Ask again later\n";
    } else if (answer == 6) {
      cout << "The answer is: Don't count on it\n";
    } else if (answer == 7) {
      cout << "The answer is: My sources say no\n";
    } else {
      cout << "The answer is: Reply hazy, try again\n";
    }

    cout << "Would you like to play again?(y/n): ";
    
    while(play != "no" && play != "n"&& play != "y" && play != "yes")
    {cin >> play;

    if (play == "no" || play == "n") {
      cout << "Thank you for playing with the Magic 8 Ball";
    } else if (play != "no" && play != "n"&& play != "y" && play != "yes") {
        cout<<"you enter an unknown value, try agein"<<endl;
    }
   }
   }
    return 0;
   }

i declare (string play) then i reassign it so it could not save the y/n answer from round to round cuz that would break the program and i add a loob down the code that will get you y/n answer more effective

CodePudding user response:

You have to add

getline(cin, question);

If you don’t,

cin >> question;

won’t be able to read more than one word.

CodePudding user response:

you need to create a loop with a while or a for

  •  Tags:  
  • c
  • Related