Problem with totaling numbers that was spinned by a dice

This is a discussion on Problem with totaling numbers that was spinned by a dice within the C Programming forums, part of the General Programming Boards category; hi there, I am writing a function that is suppose to calculate the total number of moves, a user gets ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    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. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    hi,
    thanks for ur help pianorain but why did it make a difference?

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    thanks for all of ur inputs

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DICE problem!
    By pode in forum Game Programming
    Replies: 8
    Last Post: 02-14-2002, 01:25 AM
  2. Totaling up numbers in a for loop?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 03:23 PM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. Problem Formatting double numbers.
    By chaps67 in forum C++ Programming
    Replies: 1
    Last Post: 09-19-2001, 09:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21