# Thread: Problem with totaling numbers that was spinned by a dice

1. ## Problem with totaling numbers that was spinned by a dice

hi there,
I am writing a function that is suppose to calculate the total number of moves, a user gets by a spin of a dice. The game board has 25 spaces in all. Suppose on the first spin a user gets a 5, a message should come up saying "You have just moved 5 spaces". If on the next spin you get a 4,a message should come up saying that "You have moved 9 spaces". Unfortunately the code is not doing this. If the user got a 5 a message comes up saying " You have just moved 5 spaces". If a 6 comes up, the message says " You have just moved 6 spaces". Its not totaling it up.

Code:
```
/* Purpose of this function is to keep track of the total numbers of spaces
player2 moves in the game*/

int player1_move_spaces(int array1[],int die) //die represents the number that the user got when the dice was rolled
{
int num;

num = num + die;

array1[num]=num;
printf("You have just moved %d spaces",num);

return num;
}```

2. If you want to do it that way, you'll need to use a static variable.
Code:
```int player1_move_spaces(int array1[],int die) //die represents the number that the user got when the dice was rolled
{
static int num = 0;
//snip```

3. hi,
thanks for ur help pianorain but why did it make a difference?

4. The static keyword can mean different things depending on how it's used. I'm not up for discussing all of them. Using the static keyword when defining a variable in a function tells the function not to delete that variable when it goes out of scope. Example:
Code:
```int foo()
{
int a = 0;
a++;
return a;
} //this function will return 1 every time
//because a is deleted when it goes out of scope.

int bar()
{
static int a = 0;
a++;
return a;
} //this function will return an incremented value every time
//because a is not deleted when it goes out of scope.```

5. Automatic variables local to a function are stored in the stack. When the function is called the stack grows to accomodate those variables. When the function returns the stack is shrunk back down to its original position. Every time the function is called the process is repeated. Since those variables are getting recreated every time the function is called it doesn't remember what the value of the variables was from the last call.

But static variables local to a function are stored in a different section of memory that remains intact throughout the life of the program. So even when the function returns and is called again, it will remember the value from the last time the function was called.

You should also make sure you initialize variables. When the stack grows for the automatic variables it doesn't change the contents of that memory. So if you just have like:
Code:
```void somefunc(void)
{
int num;

printf("%d\n", num);
}```
It could print any integer. It doesn't necessarily get set to 0 unless your explicitly initialize it to that.

6. thanks for all of ur inputs

7. You don't need a static variable. Just keep track of the total number of moved squares. You're already storing the dice rolls in an array. So just sum them up:
Code:
```for( x = 0; x < num; x++ )
foo += array1[x];
printf("You've moved a total of %d spaces.\n", foo );```
Quzah.