/* ask the user if they want to play? while the answer is yes Ask the user for their question Generate a random response print the question and response ask the user if they want to play again? To Generate a random response Generate a random number between 0 and 19, or 1 and 20 return the corresponding phrase */ #include #include using namespace std; string GenerateResponse(); char AskYesNoQuestion(string question); int main() { string question, answer {"Yes"}; char response; srand(time(nullptr)); response = AskYesNoQuestion( "Do you have a question for the magic 8-ball (Y-N)? "); while ('Y' == response) { cout << "Enter a question for the magic 8-ball: "; getline(cin,question); cout << endl; answer = GenerateResponse(); cout << "Your question: \"" << question << "\"" << endl; cout << "The magic 8-ball says: " << answer << endl; cout << endl << endl; response = AskYesNoQuestion( "Do you have anoter question for the magic 8-ball (Y-N)? "); } cout << endl; cout << "Thank you for using the magic 8-ball" << endl; return 0; } char AskYesNoQuestion(string question){ char response {'x'} ; while (response !='Y' and response != 'N') { cout << question; cin >> response; cin.ignore(100,'\n'); response = toupper(response); } return response; } string GenerateResponse(){ string response; int dieRoll; dieRoll = rand() % 12; switch(dieRoll) { case 0: case 4: case 11: response = "It is Certain"; break; case 1: case 5: case 10: response = "It is decidedly so."; break; case 2: case 6: case 9: response = "Reply hazy, try again."; break; case 3: case 7: case 8: response = "Don't count on it."; break; default: response = "There is an error in the program."; } return response; }