Cleaning up the offending code

Hello again,

so here is how I cleaned it up and separated the Guet Guess and Print Guess.

I made another String Method, mainly because both were under a string before that, yet it got me wondering when should I use a void… I had to play around a bit with the actual “string Guess =” statement for the PrintGuess method, it seemed logical to give it the result of the Get Guess, I am not sure if I set the value the right way or if there is another way but it works in the end.

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
string PrintBack();

//The entry point of our application
int main()      
{
	PrintIntro();

	PlayGame();
	
	return 0; // exit the application
}

// loop for the number of turns asking for guesses
void PlayGame()
{
	constexpr int NUMBER_OF_TURNS = 5;

	for (int count = 1; count <= NUMBER_OF_TURNS; count++)
	{
		GetGuess();
		PrintBack;
		cout << endl;
	}
	return;
}


// introduce the game
void PrintIntro()   
{
	constexpr int WORD_LENGTH = 9;
	
	cout << "Welcome to Bulls and Cows, a fun word game.\n";
	cout << "Can you guess the " << WORD_LENGTH << " letter isogram I'm thinking of?\n\n";
	return;
}

// get and guess from the player and print it back
string GetGuess()
{
	string Guess = "";		//First char Uppercase value for Unreal standards

	cout << "Enter your Guess: ";
	getline(cin, Guess);
	return Guess;
}

// Print Guess
string PrintGuess()
{
	string Guess = GetGuess();

	cout << "Your guess was: " << Guess;
	cout << endl;
	return Guess;
}

Cheers!

Actually something wrong happen here, I notice PrintGuess but also PrintBack in the code, I corrected it and changed everything to PrintGuess, and now it is not working properly.

Debugging…

Well, something weird happened there… but hey, last resort…continue the video. I gave it a try my approach was wrong. I learned another :slight_smile:

#include <iostream>
#include <string>

using namespace std;

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

//The entry point of our application
int main()      
{
	PrintIntro();

	PlayGame();
	
	return 0; // exit the application
}

// loop for the number of turns asking for guesses
void PlayGame()
{
	constexpr int NUMBER_OF_TURNS = 5;

	for (int count = 1; count <= NUMBER_OF_TURNS; count++)
	{
		string Guess = GetGuess();
		cout << "Your guess was: " << Guess; // print back the guess
		cout << endl << endl;		
	}
}

// introduce the game
void PrintIntro()   
{
	constexpr int WORD_LENGTH = 9;
	
	cout << "Welcome to Bulls and Cows, a fun word game.\n";
	cout << "Can you guess the " << WORD_LENGTH << " letter isogram I'm thinking of?\n\n";
	return;
}

// get and guess from the player and print it back
string GetGuess()
{
	string Guess = "";		//First char Uppercase value for Unreal standards

	cout << "Enter your Guess: ";
	getline(cin, Guess);
	return Guess;
}

This now works well !

PS: by the way my code in the IDE is properly space with Tabulations etc I do not know why when IO paste it here it moves things around.

its looking good!

Thank you !

I actually did the same thing as Suraknar. I created two separate functions - GetGuess and PrintGuess. I then called them both within the play game for loop. I then had to define Guess again within PrintGuess.

I can see why this gets messy and we end up throwing print guess into the for loop, but what confused me is I expected this

//Loop for number of times asking for and returning guesses
void PlayGame()
{

constexpr int NUMBER_OF_TURNS = 5;

for (int count = 1; count <= NUMBER_OF_TURNS; count++)
{
	string Guess = GetGuess();
	GetGuess();
	cout << "Your guess was  | " << Guess << endl << endl;
}

}

But that doesnt work. It clearly runs the function GetGuess twice before returning with your guess.

I am slightly confused why “string Guess = GetGuess();” also runs the function GetGuess rather than just defining the string Guess.

I expected yes you define Guess with the return of the function GetGuess which at this time will be null, then run the function GetGuess which will cout the question and recieve the guess, then returning the player typed string to GetGuess which is now defined along with Guess from the line “string Guess = GetGuess();”

Forgive me, Im brand new to code, but I want to make sure I understand this.

From what I have gathered

If I define something (string or int or whatever) by a function, it then immediately runs the function and retains what the function returned as its … value(for lack of better word).
Is this always the case when defining anything?

Sorry if this doesnt make much sense. Best I could make of it.

Thanks

I’m not 100% sure I understand your question, but I do see what is wrong with your code. Now let’s see if I can explain it enough to help you understand what is going. I’m not sure if I have the vocabulary to do it.

The function GetGuess() returns a string. That means that when the function GetGuess is called/runs, what it’s returning/giving is the guess entered by the player. In this case it is a word. Say my guess is Donkey, that means that after the function GetGuess() runs it is now returning/giving the word donkey. That means this line of code:

string Guess = GetGuess();

is really the saying this:

string Guess = Donkey

What that means is that your second call/run of the function GetGuess() is not necessary and that is what is making your program not act correctly. If you get rid of the GetGuess() just above your cout statement it should work just fine.

string Guess = GetGuess();
GetGuess();
cout << "Your guess was | " << Guess << endl << endl;

I also do not understand why: “string Guess = GetGuess();” shows the entire output of the function GetGuess rather than just setting the string Guess to its return value.

Assuming the function GetGuess() returns “Donkey”, I would have thought this code:
string Guess = GetGuess();
cout << "Your guess was: " << Guess << endl;

Would output: “Your guess was: Donkey”

However, it outputs:
"Enter your guess: "
“Your guess was: Donkey”

I also tried making two functions for the get and reply.
I split it up and then XCode started crying that it didn’t know what Guess was in the two new lines I made.
It was all “Use of undeclared identifier ‘guess’”

Then I was all like WHAAAA- What are you TALKING about, XCode!? I identified you!
And then I came on here and found that like, others did the same thing and it’s all cool.

I’m still not clear on WHAT exactly the “offending code” is cause like, none of the code is offending anything. It still works, right? Lol I don’t get it, but I’m a newb too.

I found out that instead of creating a separate function, just make the reply in the Play Game part of things so that it just takes care of itself there cause’ otherwise it’s clunky and redundant. Kind of like how a pelican feels when it swallows a fish and the fish just keeps trying to fight it’s way back up and out.

Wouldn’t it be a lot more efficient and cleaner if you didn’t repeat the variable Guess more than once? Here is what I did for my cleanup

void GetGuess()
{
cout << "Enter your guess: ";
return;
}

string PrintGuess()
{
string Guess = “”;
getline(cin, Guess);
cout << "Your guess was: " << Guess << endl;
return Guess;
}

Then for the for loop I just did this:

constexpr NUM_GUESSES = 5;
for(int count = 0; count < NUM_GUESSES; count++)
{
GetGuess();
PrintGuess();
cout << endl;
}

It seems to work just fine not sure if I’m right or not, but another suggestion to help out :slight_smile:

Hey there,

this is how I “cleaned up” my code. I found it a pretty mess to have so many voids and strings, so I decided to just condense the whole code for GetGuess and PrintGuess in the “for-loop”-function which I then named PlayGame.

#include
#include

using namespace std;

void PrintIntro();
void PlayGame();

// the entry point for our application

int main() {

PrintIntro();
PlayGame();

return 0;

}

// introduce the game

void PrintIntro() {
constexpr int WORD_LENGTH = 4;
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;
}

// Function of Game

void PlayGame() {
for (int count = 1; count <= 5; count++) {
// get a guess from the player
cout << "Enter your guess: ";
string Guess = “”;
getline(cin, Guess);
// repeat the guess back to them
cout << "Your guess was: " << Guess <<endl;
cout << endl;
}
cout << endl;
}

for me this seems much more space-efficient… What are your thoughts? I would be happy with any critics or suggestions of any problems that can come out of this.

This is my FOR loop:

void PlayGame()
{
// loop for the number of turns asking for guess
constexpr int Number_of_turns = 5;
for (int count = 1; count <= Number_of_turns; count++)
{
string Guess = GetGuess();
cout << "You entered: " << Guess << endl;
cout << endl;
}
}

It looks like it works without including a return; at the end.

I also was confused about the fact that after I declared string Guess = GetGuess();, I didn’t have to run the function GetGuess();, because it actually ran when I attributed it to Guess. This is very strange to me, but I have to accept this as a truth: When you attribute a function to a variable, the program actually runs the function in order to take the value and attribute it.

I hope I got nothing wrong. I really want to get the basisc of C++ programming right, so I will not have trouble with the syntax later.

Privacy & Terms