Advertisement

one helluva logical error....

Started by November 10, 2000 08:56 AM
2 comments, last by Gaiiden 24 years, 2 months ago
OK, this has me completely stumped (for the time being). So whilst I sit in front of my laptop and try to figure this monster out, let''s see who can beat me to it. Thing is, since I don''t know how to debug in C++ that well (I have a monochrome monitor, but can''t hook it up to my laptop - fooey) I just use message boxes. Stick after a line I think is having a problem. If error pops up before the message box - Bingo! Works pretty well.... Anyways, I have this funcion that I use to print text out to the screen. It automatically moves down a line and prints every new text line so i don''t have to worry bout that. Well, when I print out a message, the function seems to go in this weird loop - even if I have a dialog box statement in there it pops up once, then, although I can hear the sound it makes, it doesn''t show up again. But the sound keeps going off Bring, Bring, Bring... So what''s going on? Well, I have two windows timers I use. I stuck a MessageBox() statement at the top of one and viola! All the text printed line by line with no weird looping! So what''s the problem? Well, besides having to stick in a friggin MessageBox() for no real reason - the weird looping takes place even BEFORE I''ve created the timer with SetTimer()! So why should it make a difference?? Also, there are no loops whatsoever in my text function, and no chance of recursion or anything like that. I don''t know why that MessageBox() statement does what it does, it doesn''t even execute until the timer starts (and then it does appear every second like its supposed to). OK, while you think about this (and laugh at me) I''m gonna go back to figuring this dumb thing out.... ============================== "Need more eeenput..." - #5, "Short Circuit" ==============================

Drew Sikora
Executive Producer
GameDev.net

Damn I''m good.

Ok, I solved the problem. Here''s the deal. This is the code I had in my timer message handler:
  case WM_TIMER:	// a timer has elapsed  {    // what timer?    switch(wparam)    {      case CONN_TIMER:        {	  // update and test to see if connection has timed out	  if (conn >= TIME_OUT)	  {	    UpdateUser(IDS_ERR_CONNECT, BLUE, ADVANCE);	    CleanUp();          }	  conn++;	} break;  

Now, notice the conn++; see it? If I move that line to the top of the case statement (right above the if statement) Then i get this weird looping effect described in the first post. Don''t ask me why - I have no friggin idea. Anyone want to venture a guess??


==============================
"Need more eeenput..."

- #5, "Short Circuit"
==============================

Drew Sikora
Executive Producer
GameDev.net

Advertisement
Because

If you increment conn before you do your test, your if statement will always be true.

Look here

conn = 0
TIME_OUT = 1

conn++
if(conn >= TIME_OUT)
{ do stuff
}

....

As you can see, conn will always be larger this way. and will always do the if code. By placing it outside, the if will only be done at the right time. It is usually good practice to do your work first, then prepare for the next loop. Look at this.

set up for first run,
do first run,
set up for second run,
loop.

This is good pratice. Hope this helps

Kevin =)
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
sound advice - EXCEPT that TIME_OUT is a constant that equals 15, and why the hell are these statements executing at all?? The error came before the timer was ever created!

Oh, and in other news, I''ve found its my WindowRepaint() function that is supposed to redraw all the text on the screen that''s causing the weird looping. See, I have a string table right? So every string I print out, I store it''s number ID into an array. Then, when the window needs repainting, I redraw all the text by pulling their numbers out of the array one by one, loading them, and then outputting them. But it''s giving me trouble.



==============================
"Need more eeenput..."

- #5, "Short Circuit"
==============================

Drew Sikora
Executive Producer
GameDev.net

This topic is closed to new replies.

Advertisement