weirdest logical error i've ever run into

• 05-25-2003
Leeman_s
weirdest logical error i've ever run into
Ok, there has to be a MAJOR logic error made by me here. With the following code, the message boxes cycle through: "playflag is true", "playflag is false", "we have a match", and it goes through that cycle about 5 times. After you look at the code, you'll see that after playflag is set to true, it should never get inside that chunk of code again, unless playflag is somehow set to false again. And of course, the only time I see the message "playflag is true" is when it gets inside that chunk of code it should never go into again. and i see "playflag is true" multiple times. which makes it seem to me, that playflag is being set to false where i don't want it to be, or something like that. see what i mean its confusing...and really messing up my program as well....have a look see if you can identify my logic error:
Code:

```void CheckTimes(char sometime[6], HWND hList) {         char* temp[1000];         for(int i=0; i<1000; i++)         {                 temp[i] = new char[50];                 strcpy(temp[i], ".");         }         i=0;         for(int x=0; x<1000; x++) //Trouble loop         {                 if(strcmp(times[x], ".")!=0) //means there is real info                 {                         strcpy(temp[i], times[x]);                         i++;                 }         }         bool leeman = false;         bool playflag = false;         for(x=0; x<i; x++)         {                 if(!playflag)                         MessageBox(NULL, "playflag is false", "playflag is false", MB_OK);                 if(playflag==false)                 {                     if(strcmp(temp[x], sometime)==0) //they match                         {                             MessageBox(NULL, "we have a match", "we have a match", MB_OK); //DOES POP UP, MULTIPLE TIMES                                 playflag = true;                                 if(playflag)                                 MessageBox(NULL, "playflag is true", "playflag is true", MB_OK);                                                                 //Sleep(1000);                             for(int a=0; a<1000; a++)                                 {                                     if(!leeman)                                         {                                         if(strcmp(times[a], temp[x])==0)                                                 {                                                     strcpy(times[a], "."); //removes entry we just used up                                                         RemoveByIndex(temp[x], hList);                                                         SaveTimes();                                                     leeman = true;                                                 }                                         }                                 }                         }                 }         } }```
• 05-25-2003
kuphryn
Here is where the variable gets changed.

if(strcmp(temp[x], sometime)==0) //they match
playflag = true;

Kuphryn
• 05-26-2003
Leeman_s
I know that, but after that gets changed, it is now true. And, that line of code is inside a if statement that says if(playflag == false). So the function should never go inside that statement again.

See, its going through a loop that says:
Code:

```//try this for example for(;;) {     if(!playflag)     {         cout<<"This text should come up only once."<<endl;         playflag = true;     } }```
After playflag is set to true, it should never go inside that block of code again, until the function is called again. Thats just like it is in the function.

So whats wrong?
• 05-26-2003
Leeman_s
Well, heres the thing that stumps me. First I'll quickly explain what the program does. It's a dialog box that loads times from a text file into a list, then saves them back to the text file as the user alters the list. I want the function CheckTimes() to be running in idle time and it checks each time in the list to the current time. If the times match, I want the messagebox to pop up.

Ok, so here are some tests I ran.

Test #1) Put CheckTimes() under case default of BOOL CALLBACK DIALOGPROC or whatever that function is called. When I do this, the message box pops up a bunch of times. This leads me to believe either, since the function is being ran many times over, during the minute (the program only records times out to the minute) it finds a match each time it runs until the next minute. Proceed to test #2.

Test #2) I put CheckTimes() under the dialog init, so it only runs once. I open the program, add in the current time, close it, open it while its still on the same minute, and I get ONE messagebox popping up like what is supposed to happen. Ok, so now I know that nothing inside the function is making the messagebox pop up a bunch of times, its probably because when its under default it runs a bunch of times. BUT, when the function is ran, the entry is removed. I've tested this and I know the removing works.

Test #3) Under the dialog init, I put checktimes() to run 20 times in a for loop. Now, this would be like the function running many times under case default of the windows function BOOL CALLBACK blah blah. BUT, to my surprise, the messagebox comes up only once like I want it to. So, this really confuses me.

Do you get why the 3 tests confuse me?
• 05-26-2003
ripper079
Although I donīt se any "logical" erros in your code my guess would be that the variable is changed somewhere else (I know it sounds stupid). My best guess would be that the 2 functions RemoveByIndex(temp[x], hList); and SaveTimes(); has something to do with it. What happend if you comment out thoose two lines???

P.S. No good at Windows programming.
• 05-26-2003
Hunter2
Hmm, not sure what the problem is, but I've noticed that a lot of mysterious and unexplained problems I have can be fixed with the "rebuild all" button in msvc++. Just a suggestion ;)
• 05-26-2003
Leeman_s
Ok, well I fixed it by breaking it all into 3 smaller functions. Less lines of code, and it works now too :)