Different way to AskToPlayAgain usiong Switch statement and issue

Hi

When you presented the question how can we ask the player if he wants to play again I stopped and thought how could I do this with what I already know of c==

So I thought about using a switch, like so

void AskToPlayAgain() {
cout << "Would you like to play again? (y/n) " << endl;
char answer;
cin >> answer;
switch (answer) {
case ‘n’: cout << “\nThanks for playing! \n”;
break;
case ‘y’: main();
break;
default: cout << “\nPlease answer y or n \n” << endl;
AskToPlayAgain();
break;

}

}

This works nicely however once I reply y to the question I get the following

Welcome to Bulls & Cows
Can you guess the 5 letters word I am thinking of? It has no repeating letters…

Enter your guess: Your guess was

Enter your guess:


The first Enter your guess: Your guess was appears there and not sure why?


My code in full

#include
#include
using namespace std;

void PrintIntro();
void PlayGame();
void ReturnGuess(string Guess);
string GetGuess(); // a prototype of GetGuess at the top; returns a string
void AskToPlayAgain();

// the entry point of our application
int main() {
PrintIntro(); // this is a function (method or routine) call
PlayGame();
AskToPlayAgain();
return 0;
}

void PrintIntro() { // introduce the game
constexpr int WORLD_LENGHT{ 5 };
cout << “\nWelcome to Bulls & Cows \n”;
cout << “Can you guess the " << WORLD_LENGHT;
cout<<” letters word I am thinking of? It has no repeating letters…\n";
cout << endl;
return;
}

void PlayGame() { // plays game
constexpr int TURNS_NUMBER{ 5 };
for (int i{ 1 }; i <= TURNS_NUMBER; i++) {
string Guess = GetGuess();
ReturnGuess(Guess);
}
return;
}

string GetGuess() { // get a guess from the player
cout << "Enter your guess: ";
string Guess{ “” };
getline(cin, Guess);
return Guess;
}

void ReturnGuess(string Guess) {
cout << "Your guess was " << Guess << endl;
cout << endl;
return;
}

void AskToPlayAgain() {
cout << "Would you like to play again? (y/n) " << endl;
char answer;
cin >> answer;
switch (answer) {
case ‘n’: cout << “\nThanks for playing! \n”;
break;
case ‘y’: main();
break;
default: cout << “\nPlease answer y or n \n” << endl;
AskToPlayAgain();
break;

}

}

Privacy & Terms