#include #include #include using namespace std; const int ANSWER_COUNT = 20; int main() { string question, answer, playAgain; bool keepPlaying = true, goodValue = false; int randValue, i; srand(time(NULL)); while(keepPlaying) { cout << endl; cout << "Enter a question for the magic 8 ball => "; getline(cin, question); // I'm going to store the rand value in a variable for // debugging purposes. randValue = rand() % ANSWER_COUNT; // convert the random number to a string. switch(randValue) { case 0: answer = "It is certain."; break; case 1: answer = "It is decidedly so"; break; case 2: answer = "Without a doubt"; break; case 3: answer = "Yes, definitely"; break; case 4: answer = "You may rely on it"; break; case 5: answer = "As I see it, yes"; break; case 6: answer = "Most likely"; break; case 7: answer = "Outlook good"; break; case 8: answer = "Yes"; break; case 9: answer = "Signs point to yes"; break; case 10: answer = "Reply hazy try again"; break; case 11: answer = "Ask again later"; break; case 12: answer = "Better not tell you now"; break; case 13: answer = "Cannot predict now"; break; case 14: answer = "Concentrate and ask again"; break; case 15: answer = "Don't count on it"; break; case 16: answer = "My reply is no"; break; case 17: answer = "My sources say no"; break; case 18: answer = "Outlook not so good"; break; case 19: answer = "Very doubtful"; break; // if you add a new case, make sure to change ANSWER_COUNT default: answer = "Error, the 8 ball is really confused."; } // print the answer cout << "You asked: \"" << question << "\"" << endl; cout << "The 8 ball replies: \"" << answer << ".\"" << endl; cout << endl; cout << endl; goodValue = false; // find out if the player wants to play again. while (!goodValue) { cout << "Do you want to ask another question? (yes/no) => "; getline(cin,playAgain); // make the entire string upper case. i = 0; while(i < playAgain.size()) { playAgain.at(i) = tolower(playAgain.at(i)); i++; } // make a decision based on the answer. if (playAgain == "yes") { goodValue = true; cout << "Ok, we will keep playing" << endl; } else if (playAgain == "no") { goodValue = true; keepPlaying = false; cout << "Ok, we will stop playing" <