Thread: VS2005 bug!! or not...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    VS2005 bug!! or not...

    hi

    I'm working on a C++ project and i'm using VS2005 win32 console.

    What i'm trying to do is, draw a soccer field and place 5 players of each team in there field area. Nothing wrong here (i think).

    The problem is then i run the project. There are no compile errors, but when the soccer field appears on the win32 console it only shows 3players and not 5.
    But the funny thing (well...not so funny) is when i do a debug, the soccer field appears on the win23 console with all the 5players :-S

    why does that happen?

    Anyway, i tryed something like this:

    The order of placing the players on the field was:
    1. Goal Keeper
    2. Defender 1
    3. Defender 2
    4. Forwarder 1
    5. Forwarder 2

    And i mixed up that order with something like this:
    4.
    1.
    3.
    5.
    2.

    And now all the players appear on the field :-S

    Can anyone help me with this "bug" ?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    It's most probably not a bug.

    You must be relying on dangling pointers, uninitialized variables, or something like that somewhere.

    Go through your code and make sure that you're not doing something stupid.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The debugger will let you view your array so you know what is going on. Set a breakpoint during the display of your soccer field array and examine the array in the debugger. You can also mouse over the array name in the editor and the debugger will stick a drop down so you can browse the array. As well you can mouse over other variables in question.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sure, we can help you. How would you like us to proceed?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  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
    Set the compiler to Warning Level 4 for a start.

    It's true this was a disaster in VC6, but at least Microsoft have cleaned up all their headers in the .net compilers to allow the maximum warning level to be used effectively.

    I agree with noodles, look for uninitialised things first.
    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 IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi guys! thanks for the help...

    I'm not using pointers, and i checked for uninitialized variables. The only variable that wasn't initialized was a string and even so i gave it a initial value, but the problem continues

    At first i thought that the problem was something related to the players coordinates, because i'm using a random function to place them on the field. So i did a debug to see if the players were overlapped. But everything on the array was correct, all the players were on the right place. And the prof of that was shown at the last debug breakpoint when viewing the field on the console.

    By the way, how do i set the compiler to Warning Level 4 to give it a try?
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi once again...

    I said earlier that the problem was resolved by initializing some variables, but it wasn't the case. I still had the player insertion orders altered, so i thought the problem was solved

    In other words...problem continues
    Last edited by IndioDoido; 11-22-2007 at 04:59 PM.
    "Artificial Intelligence usually beats natural stupidity."

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Try to remove unnecessary code a little at a time until you get a small amount of code that still produces the same problem. Then post that code.

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    function that inserts the players on the field:
    Code:
    int num = 0;
    	string nEq1 = "Sporting", nEq2 = "Porto", idJogador;
    	campo c1;
    	ponto p;
    	
    		c1.eq1.setNome(nEq1);
    
    		c1.eq1.setNomeMCampo("esquerdo");
    		c1.eq2.setNomeMCampo("direito");
    
    		//player1
    		idJogador = nEq1.at(0);
    		idJogador += "1"; 
    		guardaRedes r1(1,0,0,idJogador);
    		c1.eq1.insere(r1);
    
    		p = r1.posicaoVaga(c1.campoJogo, c1.eq1.getNomeMCampo());
    		r1.setPosL(p.x);
    		r1.setPosC(p.y);
    		c1.campoJogo[r1.getPosL()][r1.getPosC()]=r1.getID();
    		
    		//player2
    		idJogador = nEq1.at(0);
    		idJogador += "2";
    		defesa d2(2,0,0,idJogador);
    		c1.eq1.insere(d2);
    
    		p = d2.posicaoVaga(c1.campoJogo, c1.eq1.getNomeMCampo());
    		d2.setPosL(p.x);
    		d2.setPosC(p.y);
    		c1.campoJogo[d2.getPosL()][d2.getPosC()]=d2.getID();
    
    		//player3
    		idJogador = nEq1.at(0);
    		idJogador += "3";
    		defesa d3(3,0,0,idJogador);
    		c1.eq1.insere(d3);
    
    		p = d3.posicaoVaga(c1.campoJogo, c1.eq1.getNomeMCampo());
    		d3.setPosL(p.x);
    		d3.setPosC(p.y);
    		c1.campoJogo[d3.getPosL()][d3.getPosC()]=d3.getID();
    
    		//player4
    		idJogador = nEq1.at(0);
    		idJogador += "4";
    		atacante a4(4,0,0,idJogador);
    		c1.eq1.insere(a4);
    
    		p = a4.posicaoVaga(c1.campoJogo, c1.eq1.getNomeMCampo());
    		a4.setPosL(p.x);
    		a4.setPosC(p.y);
    		c1.campoJogo[a4.getPosL()][a4.getPosC()]=a4.getID();
    
    		//player5
    		idJogador = nEq1.at(0);
    		idJogador += "5";
    		atacante a5(5,0,0,idJogador);
    		c1.eq1.insere(a5);
    
    		p = a5.posicaoVaga(c1.campoJogo, c1.eq1.getNomeMCampo());
    		a5.setPosL(p.x);
    		a5.setPosC(p.y);
    		c1.campoJogo[a5.getPosL()][a5.getPosC()]=a5.getID();
    if i change the order of insertion, the players will all appear in the field.


    random function (different random value for each player type)
    Code:
    ponto atacante::posicaoVaga(string campJogo[20][37],string nomeCampo)const
    {
    	int posL = 0, posC = 0;
    
    	srand ((unsigned) time (NULL));
    	posL = rand () % 20;
    	posC = rand () % 6;
    	ponto p;
    
    	while (campJogo[posL][posC] != "  ")
    		if (campJogo[posL][posC] == "  ")
    			break;
    		else
    		{
    			srand ((unsigned) time (NULL));
    			posL = rand () % 20;
    			posC = rand () % 6;
    		}
    
    	if (nomeCampo == "esquerdo")
    	{
    		
    		p.x = posL;
    		p.y = 17 - posC;
    	}
    	else if (nomeCampo == "direito")
    	{
    		p.x = posL;
    		p.y = 18 + posC;
    	}
    	return p;
    }
    What am i doing wrong? (to not say stupid...)
    "Artificial Intelligence usually beats natural stupidity."

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    These 3 lines are redunadant. Your program will work the same if you delete them:
    Code:
    		if (campJogo[posL][posC] == "  ")
    			break;
    		else
    This line
    Code:
    	srand ((unsigned) time (NULL));
    should not be in that function. It should only ever be called once in your program. This means that line should be shifted to 'main'. Please read the FAQ about this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by iMalc View Post
    This line
    Code:
    	srand ((unsigned) time (NULL));
    should not be in that function. It should only ever be called once in your program. This means that line should be shifted to 'main'. Please read the FAQ about this.
    I hope you allow me to be pedantic for a bit, since this most likely outside of the scope of the what the OP is doing.

    srand() might need to be called once per thread. I think this is system specific, however, and some systems require it once per process and others require it once per thread. Fun stuff no doubt.

  12. #12
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    The problem was on the srand ((unsigned) time (NULL));
    I removed it from all the player functions, and now it's only called once on the main function.

    Thanks allot iMalc!
    I really thought that i needed to call the srand() every time i used rand()
    "Artificial Intelligence usually beats natural stupidity."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  3. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  4. VS2005 Professional & designated initializers
    By Hansie in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 12:26 AM
  5. wierdest (annoying) C++ bug
    By phantom in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 08:12 PM