My solution using global declarations-opinions?

my code here. https://gist.github.com/anonymous/7dce5987f9b8f531d571d07613640d05#file-gistfile1-txt

I made ‘string Guess’ and ‘constexpr int NUM_TURNS’ into Global declarations
then i made split GetGuessAndPrint() into GetGuess() and PrintGuess()
I made GuessLoop() to use GetGuess() and PrintGuess
and then i made PlayGame() use printIntro and GuessLoop.

any thoughts or opinions? Should i use global variables and is it safe to use them in this scope?
my code compiles and runs exactly as intended with no errors.
feedback is appreciated.

I have little experience with programming but have played around with c++ and java before.

Ok so i found out some more information on the use of globals here

new solution without global variables
this time I printGuess takes a parameter.

string PrintGuess(string name) {
cout << "your guess was: " << name;
cout << endl;
return name;
}
this gets rid of the scope problem when used in GetGuess.
The code now runs the program with the same expected behavior.
my new code here https://gist.github.com/anonymous/5dc695d4536a11d4d13f04122654c0d8

Since your PrintGuess function is making no modifications to name, (and in the context that it is being used is not using the returned argument name, it’s probably best to just declare PrintGuess as a void function (no return).

 void PrintGuess(string name) 
 {
      cout<<"your guess was: " <<name;
      cout<<endl;
 }
2 Likes

Wow thanks, didn’t see that.

Privacy & Terms