Thread: what is wrong with the logic in my loop?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    what is wrong with the logic in my loop?

    clip of program:

    printf( "Hello ;;;;;; " );
    printf( "Hello ;;;;;; " );


    function to clear everything inside the quotes:

    Code:
    int null_chars(char str[200])
    {	
    	int i,j=0, x;
    	for (i=0; str[i] != '\n' ; i++)
    	{
    		if (str[i] == '"')							
    			{
    				for( j=i+1 ; str[j] != '"'; j++)
    				{
    					str[j] = '0';
    				}
    			}
    	}
    
    }
    this causes a huge bug...I have another function that clears comments that written the EXACT same way and works perfectly...I know there's something wrong with the for loop on the inside

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to escape quotes in strings or as single characters:
    Code:
    if( str[i] == '\"' )

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    that didn't work...it says there is some breakpoint in the loop or somethiing...

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    A null character and an escape character must have a backslash before them i.e null would be '\0' and the quotes would be '\"'. Also you want to declare index variables or variables exclusive to the loop inside of the for declaration section because it minimizes thier scope, looks less confusing, and saves memory... and to me that extra for loop you have on the inside looks unnecessary.

    Here's what I would do (It may not be syntactically correct seeing as how i didn't compile it and I am not a C programmer)
    Code:
    int STRING_LIMIT = 200;
    void null_chars(char input[STRING_LIMIT])
    {	
    	for (int i,in_quotes = 0;i < STRING_LIMIT;i++)
    	{
    		if(input[i] == '\"')						
    		{
    			if (in_quotes == 0)
    			{
    				in_quotes = 1;
    			}
    			else
    			{
    				in_quotes = 0;
    			}
    		}
    		else if(in_quotes == 1)
    		{
    			input[i] = '\0';
    		}
    	}
    	/*return or print here?*/
    }
    Last edited by 127.0.0.1; 03-10-2010 at 08:34 PM. Reason: whoops, caught a logic error after I posted ><

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Thanks! it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. Can't get the loop to work
    By pass_prime in forum C Programming
    Replies: 10
    Last Post: 06-20-2005, 03:46 PM
  3. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM
  4. Whats wrong w/ my loop?? Please help?
    By aspand in forum C Programming
    Replies: 6
    Last Post: 05-30-2002, 03:42 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM