Hi all
I would appreciate help with this issue, which I think is logical but perhaps also style related…
I am trying to make PlayGame cleaner / more abstract i.e. just contain function calls rather than text (I want to encapsulate that elsewhere).
So instead of having
void PlayGame() {
constexpr int TURNS_NUMBER{ 5 };
for (int i{ 1 }; i <= TURNS_NUMBER; i++) {
string Guess = GetGuess();
cout << "Your guess was: " << Guess << endl;
cout << endl;
}
return;
}
I have
void PlayGame() {
constexpr int TURNS_NUMBER{ 5 };
for (int i{ 1 }; i <= TURNS_NUMBER; i++) {
ReturnGuess();
}
return;
}
string GetGuess() {
cout << "Enter your guess: ";
string Guess{ “” };
getline(cin, Guess);
return Guess;
}
void ReturnGuess() {
string Guess = GetGuess();
cout << "Your guess was: " << Guess << endl;
cout << endl;
return;
}
My code variation in full is at the bottom of the post
My logical interpretation of the above code is:
PlayGame calls ReturnGuess.
ReturnGuess reads Guess from GetGuess and outputs it.
GetGuess gets the guess from the player.
I am somehow satisfied with the above since it works, however I feel it is not so explicit by just looking at PlayGame, because by reading its content the above logical steps are hidden.
I would like PlayGame to call both GetGuess and ReturnGuess, like so
void PlayGame() { // plays game
constexpr int TURNS_NUMBER{ 5 };
for (int i{ 1 }; i <= TURNS_NUMBER; i++) {
GetGuess();
ReturnGuess();
}
return;
}
As to me this immediately tells me that PlayGame gets the guess, and then returns it, making the two actions explicit.
However if I write the code like that at runtime I get asked to input the guess twice. Why is it so, and is there a way to avoid this issue while making the two actionfs of PlayGame explicit?
my code in full
#include
#include
using namespace std;
void PrintIntro();
void PlayGame();
void ReturnGuess();
string GetGuess();
int main() {
PrintIntro(); // this is a function (method or routine) call
PlayGame();
return 0;
}
void PrintIntro() {
constexpr int WORLD_LENGHT{ 5 };
cout << “Welcome to Bulls & Cows\n”;
cout << “Can you guess the " << WORLD_LENGHT;
cout<<” letter word I am thinking of? It has no repeating letters…\n";
cout << endl;
return;
}
void PlayGame() {
constexpr int TURNS_NUMBER{ 5 };
for (int i{ 1 }; i <= TURNS_NUMBER; i++) {
ReturnGuess();
}
return;
}
string GetGuess() {
cout << "Enter your guess: ";
string Guess{ “” };
getline(cin, Guess);
return Guess;
}
void ReturnGuess() {
string Guess = GetGuess();
cout << "Your guess was: " << Guess << endl;
cout << endl;
return;
}