Help with variable in different functions

Hello. I had my first real contact with programming in this course. Because of that, I found more interesting to make a program in parallel to the Bulls & Cows game (which I’m making too). I believe making my own game without looking nowhere else could help me to learn more quickly.

My Bulls & Cows game is doing ok, but my other game that I named NFL Quiz, I’m having a little trouble. I know that we can’t use the same variable in two different functions. In this video (at 8:15), the teacher show us how to correct this but in my case I’m struggling a lot and aprecciate some help.

Lines 30 and 77 are my problem!
PS. The program is incomplete of course, don’t mind about the rest.

My code:

The simplest way to solve this is to take line 27:

constexpr int NUMBER = 7;

and put it in (for example) line 9. So that you declare it outside of the functions.
Then its a global variable and can be accessed by all your functions.

that’s dirty code though and shouldn’t be how you do it.
If you already know how to pass a parameter to a function use that way instead !


As a sidenode:

bool EndGame() 

uses

return;

therefore it does not return a boolean, but it returns nothing ( void ) like IntroGame() and PlayGame().

Make it:

void EndGame()

and you’re good to go.

It worked! Thx very much!

I noticed that on line 42, in the for loop you’re not asking all the questions, but all
the questions minus one.

you have:
for (int q = 1; q < Questions; q++)

instead it should be:
for (int q = 1; q <= Questions; q++)

1 Like

You may also consider that when declaring the player has won, you never ask a question but you imply that there is one unasked.

1 Like

Privacy & Terms