How to avoid global variables

Hey everyone! I've just started learning c++, and I've came to an issue when trying to avoid using a global variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Example

static int numberOfAttempts = 0;
static int totalNumberOfAttempts = 5; 

Function 1:
while(totalNumberOfAttempts != numberOfAttempts) {
blah blah blah...
numberOfAttempts++;}

Function 2:
void printNumberOfAttempts(){
	if((totalNumberOfAttempts - numberOfAttempts) > 1){blah blah blah...}
}


How would I go about avoiding global variables in general and specific to this particular situation? Thanks!
First of all I do not see any reason to declare numberOfAttempts as a global variable. It can be used as a local variable of the function.
As for totalNumberOfAttempts if its value is not changed during the program execution then it is better to define it as a const variable. In this case that is then a variable is a const it is not important whether it is global or not though in any case it is better to make its declarative region as small as possible.
Thank you! It was a silly question :P
Topic archived. No new replies allowed.