Solution with passing Guess in the function

I know that using namespace can cause problems later on so I started from the start just writing everything in long form

#include
#include

//using namespace std;

// User Functions
void PrintIntro();
void PlayGame();
std::string GetGuess();
void PrintBack(std::string);

int main()
{
	PrintIntro();

	PlayGame();

	return 0;
}



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

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

	return;
}

std::string GetGuess()
{
	// get a guess from the player
	std::string Guess = ""; //defining Guess
	std::getline(std::cin, Guess);

	PrintBack(Guess);

	return Guess;
}
void PlayGame()
{
	//Loop for the number of turns allowed
	constexpr int NUMBER_OF_TURNS = 5;

	for (int count = 1; count <= NUMBER_OF_TURNS; count++)
	{
		GetGuess();
	}
}

void PrintBack(std::string Guess)
{
	//repeat the guess back
	std::cout << "You guessed " << Guess << "!" << std::endl;
	std::cout << std::endl;
	return;
}
1 Like

I spent about and hour or so rummaging through Google and CPlusPlus.com trying to figure this out. After that time I knew that I needed to pass something somewhere, but I couldn’t figure it out. Finally, after looking at your code, It made so much sense! Even though you didn’t explain what you did, I was able to figure it out by looking at your code. THANK YOU SO MUCH!

Privacy & Terms