Thread: Function working wrong :/

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16

    Function working wrong :/

    I have a problem here! I have this function: LINK But my function aint working! but first of I have in the construcktor to the class that the function is part of I have done this LINK so I get to the problem straight away...

    Anyway I end up getting bricksnumber[] getting bigger than 3! And I dont add values to that except for the one I do if bricksnumber is LESS than 3 ! So how is that possible? Iam still pretty new so I might have missed something!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Pastebin is annoying like this.

    Code:
    	if(bricks[x1]-1 == 0) //The table is built up with nine slots and the player enters from 1-9, and to make it work with my array I minus one from it so it starts from 0
    	{
    	if (bricksnumber[player] < 3) //checks that the player has less than 3 bricks
    		bricks[x1-1] = briktype;
    		bricksnumber[player]++;
    		checkwinner();
    	}
    Could have unintended evaluation because of your tabbing.

    Code:
    	if(bricks[x1]-1 == 0)
    	{
    		if (bricksnumber[player] < 3)
    		{
    			bricks[x1-1] = briktype;
    		}
    		bricksnumber[player]++;
    		checkwinner();
    	}

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hard to tell unless you post the actual game class where all those variables are declared.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16
    All files here! But they are danish..

    http://cpp.sourceforge.net/?show=13809 main.cpp
    http://cpp.sourceforge.net/?show=13810 game.cpp
    http://cpp.sourceforge.net/?show=13811 game.h (here is the class...)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (int x=0;x>8;x++)
    Never going to happen in winnercheck
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16
    Quote Originally Posted by Salem
    > for (int x=0;x>8;x++)
    Never going to happen in winnercheck
    Ehh I dont understand what you mean

    Anyway to make it short

    As you can see the only way bricksnumber can increase is in the "less than 3" check I have, so in my head it should not be able to rise to 4, but it DO sometimes so Iam totaly lost *crying like a child...*

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well x starts off at 0
    Then you test whether x is greater than 8 (which it isn't)
    Then the loop exits because the condition has failed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16
    Found one error! now it aint increasing but still bugged...

    Code:
            if (bricksnumber[player] < 3)
    		{//checks that the player has less than 3 bricks 
                bricks[x1-1] = briktype; 
            bricksnumber[player]++; 
            checkwinner(); 
    		}
    
    that one need the { and }

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16
    Quote Originally Posted by Salem
    Well x starts off at 0
    Then you test whether x is greater than 8 (which it isn't)
    Then the loop exits because the condition has failed.
    uhh thx stupid syntax error

  10. #10
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    technically not a syntax error :-)

  11. #11
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16
    anyway I think my code works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM