Why create a variable?

Hello there,

I was at section 2, lecture 26 and after many times I got annoyed about 1 thing lol

As an example, we were implementing GetCurrentTry() that is in a class FBullCowGame with instance BCGame inside the GetGuess() function.
For that Ben went and, again, created a local variable for it even if he is not changing it ever.

So I did this:
std::cout << "Try " << BCGame.GetCurrentTry() << “: What is your guess?” << std::endl;
std::string Guess;
std::getline(std::cin, Guess);
between other things and Ben went and did:
int CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try " << CurrentTry << ". Enter your guess: “;
std::string Guess = “”;
std::getline(std::cin, Guess);

So what I wonder is 2 things:

  1. Why do local variables when the value is being managed inside the class? Isn’t more efficient my way or is it wrong?
  2. Why initialize all variables? like Ben initializes “Guess” while I dont see the point since it will be initialized the next line with getline()


Hi, and sorry for the long delay in replying. Thanks for your questions.

It’s a little hard to read your code, it would really help if you use the code formatting feature of this forum.

I am just reinforcing the use of variables here, and I always initialise them out of good practice.

Enjoy the course

Privacy & Terms