Missing Guess Identifier?

I am very new to any coding and have no clue how to fix this i have tried for quite a long time so if someone could help me that would be great :slight_smile:

So correct me if I am wrong, functions “trap” (I dont know any fancy terms sorry) an identifier in the brackets correct? Meaning, you could not use “Guess” as i have written outside of the function. If this is true, how would I make it so both sets of getting the guess and returning the guess run? I am getting the attached errors, i believe the GetLine is similar in that it is trapped within the function. Thanks in advance.

I’m new to coding too so i’m not sure if this is right, but I think that you need to define Guess as a string because Guess was defined a string only for the string GetGuess() function. Alternatively, you could move the code in lines 18-25 to the GetGuess() function. Also don’t forget to return GetGuess() function as a string.

Yea, i was thinking of doing the second thing you said, but decided against it because i want to know how to do this as well it seems important. How would i define Guess outside of the string?

You’re right by saying that Guess is ‘trapped’ inside the GetGuess function (the correct term would be ‘scoped’ i.e. you can paraphrase this like so - being visible inside the function scope only). Anyways, to fix this code, first, like mentioned above, return the Guess from GetGuess (i.e. between the lines 45 and 46 add another line)
return Guess;

And then in your main() function ‘capture’ that return value. Change line 15 like this:

string Guess = GetGuess();

Just to clear up the possible confusion - you don’t have to call them Guess in all cases, e.g. in main() instead of Guess you can call it TheReturnedGuess. Although you may have already known about this.

Okay, thanks for the response. I realize i had to add return Guess at the end of the string, so what your saying is to add the line you wrote above to under return Guess? And the last thing you said, you just mean you can name it whatever you want correct?

Okay sorry you said where to put it didnt see that at first. But i have another question if you dont mind, does String Guess=GetGuess(); just identify what GetGuess is?

inside main() GetGuess(); is visible because you declared it on line 7 inside the global scope. Sorry if I may not have understood your question well?

So the previously mentioned line of code makes “Guess” avalaible throughout the solution?

Yes, as GetGuess is a global function you can call it and access the user’s guass throughout the project.

Privacy & Terms