Question: Need some help here, when I run this program, it will print out the statement You have chosen to Quit! twice before it actually closes.
Need some help here, when I run this program, it will print out the statement "You have chosen to Quit!" twice before it actually closes. Same with "I do not recognise that option, try again!" Can someone help me out? The issue is within the GameLoop.cpp and I know it's because of the IF statement on line 75, but I can't figure out a way around using it.
GameLoop.cpp
#include "GameLoop.h" #include
using namespace std;
void Game::WelcomePlayer() { cout << "Welcome to Text Adventure!" << endl << endl; cout << "What is your name?" << endl << endl;
string name; cin >> name; m_player.SetName(name);
cout << endl << "Hello " << m_player.GetName() << endl; }
void Game::GivePlayerOptions() const { cout << "What would you like to do? (Enter a corresponding number)" << endl << endl; cout << "1: Enter my preferred game console." << endl; cout << "2: Quit" << endl << endl;
}
void Game::GetPlayerInput(string& playerInput) const { cin >> playerInput; }
PlayerOptions Game::EvaluateInput(string& playerInput) const { PlayerOptions chosenOption = PlayerOptions::None;
if (playerInput.compare("1") == 0) { chosenOption = PlayerOptions::ConsoleType; } else if (playerInput.compare("2") == 0) { cout << "You have chosen to Quit!" << endl << endl; chosenOption = PlayerOptions::Quit; } else { cout << "I do not recognise that option, try again!" << endl << endl; }
return chosenOption; }
void Game::GetConsoleType() { cout << "What is your preferred game console?" << endl; string console; cin >> console; m_player.SetConsole(console);
cout << "You have chosen the " << m_player.GetConsole() << " as your preferred console." << endl << endl;
}
void Game::RunGame() { WelcomePlayer();
bool shouldEnd = false; while (shouldEnd == false) { GivePlayerOptions(); string playerInput; GetPlayerInput(playerInput); // Created IF statement to get console type if user chooses that option. if (EvaluateInput(playerInput) == PlayerOptions::ConsoleType) { GetConsoleType(); } shouldEnd = EvaluateInput(playerInput) == PlayerOptions::Quit; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
