My Code

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
void PrintGuess(string guess);

int main()
{
	PrintIntro();
	PlayGame();

	return 0; // exit application
}

void PlayGame()
{
	// loop for the number of turns asking for guesses
	constexpr int NUM_OF_TURNS = 5;
	for (int i = 0; i < NUM_OF_TURNS; i++)
	{
		PrintGuess(GetGuess());
	}
}

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

	return Guess;
}

void PrintGuess(string guess)
{
	// repeat the guess back to them
	cout << "Your guess was: " << guess;
	cout << endl << endl;
}

void PrintIntro()
{
	// introduce the game
	constexpr int WORD_LENGTH = 5;

	cout << "Welcome to Bulls and Cows, a fun word game.\n";
	cout << "Can you guess the " << WORD_LENGTH;
	cout << " letter isogram I'm thinking of?\n";
	cout << endl;

	return;
}
1 Like

Wow I didn’t think about using PrintGuess to Call Get Guess. That’s insane.

Interesting. I will try this to see if it solves my own problem.

Well, it worked, but i’m not entirely sure why…

@socratesBleu what issue were you having, that this solved?

@nilamo I wanted the player guess and the repeating of that guess as two separate functions.

these were my personal notes:

so I had two functions getGuess and returnGuess, since the string variable was local I had no idea how to grab that small bit. So I added the whole of getGuess into returnGuess which had the effect of running the getGuess function twice before showing what the player inputted.

I’d show more but I corrected the code and overwrote the save. Let me know if that made sense, if not i’ll try and go back to recreate the issue.

-C

Privacy & Terms