weirdest logical error i've ever run into

This is a discussion on weirdest logical error i've ever run into within the C++ Programming forums, part of the General Programming Boards category; Ok, there has to be a MAJOR logic error made by me here. With the following code, the message boxes ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    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;
    						}
    					}
    				}
    			}
    		}
    	}
    }
    Last edited by Leeman_s; 05-25-2003 at 06:15 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is where the variable gets changed.

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

    Kuphryn

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    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?

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    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?

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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.
    Last edited by ripper079; 05-26-2003 at 11:10 AM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Ok, well I fixed it by breaking it all into 3 smaller functions. Less lines of code, and it works now too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 07:00 PM
  2. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 04:04 AM
  3. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 12:55 PM
  4. Program to run in Dos only
    By ihsir in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 06:16 PM
  5. Trying to make rand different per program run
    By Dreamerv3 in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2002, 03:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21