-1
#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.

B0T_Mystic
  • 11
  • 2
  • 1
    Tip: Learn about `switch` – tadman Oct 01 '22 at 02:32
  • Oh, it involves switch statements? I didn't think it did, I'll use it then – B0T_Mystic Oct 01 '22 at 02:34
  • It'll reduce all that `if (... = N)` cruft into something way more tidy. Don't forget about `break;` though to avoid fall-through. This is also a candidate for a simple look-up table, which results in even *less* code. Consider: `std::vector answers = { "...", "...", ... }` and you can do `answers[n]`. – tadman Oct 01 '22 at 02:34
  • There is no *C plus plus* language. And C++ does not terminate your program; an error in your code does - you need a loop for it to keep running. Are you and [this poster](https://stackoverflow.com/q/73915422/62576) in the same class? You've posted very similar questions. – Ken White Oct 01 '22 at 02:34
  • 2
    `cin >> question;` will read one word. If you want to read a line use `std::getline`. I assume your program exits because you entered more than one word so the next input takes the second word, etc. Try stepping through in a debugger and looking at the variables after the input or print them out. – Retired Ninja Oct 01 '22 at 02:35
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 01 '22 at 02:57
  • That is also an assignment we had this week, it might be a classmate but the course is online so I wouldn't know who. – B0T_Mystic Oct 01 '22 at 03:44

2 Answers2

1

You have to add

getline(cin, question);

If you don’t,

cin >> question;

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

0

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