Solved: Problem with my solution for "Removing the sideffect" challenge

I attempted to separate the two parts of GetGuessAndPrintBack() into two different functions called GetGuess() and PrintBackGuess().

I created prototypes for each, a string for GetGuess() and a void for PrintBackGuess().

Now, when I run the code, it will GetGuess() but it will only PrintBackGuess() every other time I enter one. I’m thinking that maybe I need to change the prototype to string for the PrintBackGuess, and set the cout to a string and return said string.

So something like: string GuessSaver << "Your guess was: " << Guess << endl;
return Guess Saver;

Anywho, off to try and make this work.

So to do a little test, I appended the PrintIntro() function after my GetGuess() function within my PlayGame() function.Relevant screenshot attached. The same thing happened where it would take my guess and print nothing back, not even the intro. But on the SECOND attempt it returns the guess and prints the intro.

Something about the GetGuess() function is preventing the for loop from continuing all the way through, I suspect.

So my solution came after taking a look at some other folks code. Kevins was especially helpful as he showed that I had basically tried to call a PrintBackGuess() function without it taking any inputs.

It’s all well and good that GetGuess() was able to store an initial value as a string, but the for loop ran through all three functions without any inputs. So I nested my GetGuess function within my PrintBackGuess function like so: PrintGuess(GetGuess()), and voila.

I still can’t understand why the PlayIntro() was being skipped over the first time or why the GetGuess was only returning every other string I input, but this may be one of those higher level concepts that just don’t matter right now. Anyways here’s the completed code.

Privacy & Terms