error in my code, please help

This is a discussion on error in my code, please help within the C++ Programming forums, part of the General Programming Boards category; In this function I made, it takes input which will always be in the format: xx:xx like a time. For ...

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

    error in my code, please help

    In this function I made, it takes input which will always be in the format: xx:xx like a time. For example 17:05. In military time. times[] could possibly hold the same info somewhere. so i check all through the array times[] for a match. when there is a match, i want a messagebox to come up, which is in the code. the only problem is, the messagebox comes up a TON of times, not just once. i thought i stopped it from being done a bunch of times by using playflag. (its called playflag because eventually it will play a sound). Anyway, heres the code - any help is appreciated:

    EDIT: ooh, and this function is run many times over and over again. but you see i set the element in times[] to a "." which signifies no entry. So when it is run again, it shouldn't find anything there, i'm not sure what the problem is

    Code:
    void CheckTimes(char sometime[6])
    {
    	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 flag = false;
    	bool playflag = true;
    	for(x=0; x<i; x++)
    	{
    		if(playflag)
    		{
    		    if(strcmp(temp[x], sometime)==0) //they match
    			{
    			    MessageBox(NULL, "we have a match", "we have a match", MB_OK);
    				playflag = false;
    			    for(int a=0; a<=999; a++)
    				{
    				    if(!flag)
    					{
    				        if(strcmp(times[a], sometime)==0)
    						{
    						    strcpy(times[a], "."); //removes entry we just used up
    						    flag = true;
    						}
    					}
    				}
    			}
    		}
    	}
    }

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    I found what is not going on, but I'm not sure why. Like I said the function is ran over and over again. For some reason, the entry from times[] is not being removed when the function is ran. I use the exact same chunk of code elsewhere in my program and it works just fine. I'm not sure why it doesn't work now.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    5
    for(int i=0; i<1000; i++)
    for(int x=0; x<1000; x++)
    for(x=0; x<i; x++)


    hello, i am pretty surre that u can't use I and X outside the loops they were declared in. They get distroyed after the loop ends. I might be rong.

    You last loop might be repeating because X and I don't exist in that loop so wat u end up with is

    for ( ) <-- this is a perferctly normal c++ loop.

    if u use this kind of loop then u have to provide an exiting condition inside the code that is looped.

    so my conclusion is that since u are using variable created in the other loops they effect ur last loop. So one solution is to makee them global by declaring them after this

    char* temp[1000];

    int x,y,i;

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Nah, I've narrowed it down to the line that says:

    strcpy(times[a], ".");

    I know this code runs, because if I put a MessageBox() right above it, the message box appears. The only problem is that the correct times[] element is not being set to ".". Hmm...

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    5
    hmmm, ok since you are only trying to move one char then u can do this

    times[a] = '.' ;


    this is a more efficient method and it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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