My solution using global declarations-opinions?

my code here.

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

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;

Wow thanks, didn’t see that.

Privacy & Terms