Thread: having problems with my if statements

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

    having problems with my if statements

    hi there,
    I am creating a C boardgame, and my program isnt working very well. I narrowed it down to the function shown below. The purpose of this function is to keep track of the total numbers of spaces player1 moves in the game, how much more spaces the player has to move in order to win. Oh and it also has some validation. They are 2 of these function in my program.
    Now the problem that I am having is with the part where the function is to keep track of the total numbers of spaces player1 moves in the game and how much more spaces the player has to move in order to win.

    Code:
               int players_move_spaces(int array1[],int die2)
    {
    	
    	static int num2;
    	int left_over2;
    
    	num2=num2 + die2; //die represents the value that was obtained by spinning the dice
    			//num2 stores the total value of all the numbers obtained by spinng the dice			
    	
            array1[num2]=num2; //array moves to num2's position 
    	printf("You have just moved %d spaces",num2);
    	left_over2= SIZE - num2; //how much more spaces the player has to move in order to win. SIZE is 25 cuz they r 25 spacesin the array
    	
    	if(die2 < left_over2 )
    	{
    		printf("\n");
    		printf("You have %d more spaces to move in order to win ",left_over2);
    	}
    	else
    		if(die2 > left_over2 )
    		{
    			
    			printf("Sorry you must forfeit a turn ");
    			printf("\n");
    			printf("They are not enough spaces.  ");
    		}
    	if(num2==SIZE)
    	{
    	
    		printf(" CONGRADULATIONS !! Player 2 won the game ");
    	}
    
    return num2;
    }
    What i realise, is that the function is some times displaying 2 of the printf statements contained in first 2 if statements which is incorrect. If any help can be offered i would greatly appreciate it.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    i have been here sitting for hours and still there is no progress so i have decided to post the program. I am using Microsoft Visual Studio.NET 2003. I am trying to teach myself C, so it isnt anything fancy I am only including the .cpp file

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    move your printf statement outside the while loop.
    Code:
    printf("Player one must play first");
    while you are at it, intialize dice and dice2 to 0, otherwise you get warning messages.
    When no one helps you out. Call google();

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is the C board, so stop compiling as C++. game.cpp

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Weird function problems
    By Inquirer in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2002, 07:49 PM