Thread: loop problem

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    loop problem

    Hi all!

    The third loop in this code always runs one time, thus it just check the first "pword". I've no idea why....

    thanks in advance for help

    Code:
    struct node* Build(struct node* head)
    {
    	p = head->next;
    	while(p->next)
    	{
    		p=p->next;
    	}
    
    	pword=strtok(buf, "\n#");
    	while(pword)
    	{
    		n=malloc(sizeof(struct node));
    		char array1[strlen(pword)];
    		p->word=malloc(strlen(pword));
    		strcpy(array1, pword);
    		strcpy(p->word, pword);
    		pword=strtok(NULL, "\n#");
    		p->next=n;
    		p=n;
    	}
    	pword=strtok(buf, " \n.,#()");
    	char array2[strlen(askWord)];
    	strcpy(array2, askWord);
    	while(pword)
    	{
    		char array1[strlen(pword)];
    		strcpy(array1, pword);
    		if(strcmp(array1, array2)==0)
    		{
    			save=1;
    		}
    		pword=strtok(NULL, " \n.,#()");
    	}
    	return head; 
    }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Just a note first of all:

    Code:
    char array1[strlen(pword)];
    is a variable-length array from C99, which you may or may not want to use. You might want to use malloc() instead. Also, it should be strlen(somestring) + 1 to account for the '\0' null character at the end of the string.

    More on topic, I don't see a buf being passed to this function or buf defined within the function, is buf a global variable? Not seeing p defined either. Want to post some more code?

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Because
    strtok(NULL, " \n.,#()");
    is returning NULL.
    But what do you expect? You're passing NULL to it.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    Becauseis returning NULL.
    But what do you expect? You're passing NULL to it.
    Uhh, that's how strtok() works.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks for the comments and the ideas,

    I changed the strlens to strlen(somestring) + 1, but that didn't help.
    Here is some code from the main function...

    ...."buf" and "p" are global.



    Code:
    	save=0;
    	fgets(buf, 1000, fp);
    	while(!feof(fp))
    	{
    		if(strchr(buf, '~'))
    		{
    			if(head)
    			{
    				if(save==1)
    				{
    					Print(head);
    					save=0;
    				}
    				Delete(head);
    			}
    			head = malloc(sizeof *head);
    			p=malloc(sizeof(struct node));
    			head->next=p;
    		}
    		Build(head);
    		fgets(buf, 750, fp);
    	}

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    I solved the problem!

    It didn't work because I tried to check the same variable two times with the strtok commands. The solution is that I made one loop of the second and third loop (see the first post).

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    I changed the code to this, but it doesn't work the way I want - the second strtok is not recognized.

    Code:
    struct node* Build(struct node* head)
    {
    	p = head->next;
    	while(p->next)
    	{
    		p=p->next;
    	}
    	char array2[strlen(askWord)+1];
    	strcpy(array2, askWord);
    	pword=strtok(buf, " \n#");
    	while(pword)
    	{
    
    		char array1[strlen(pword)+1];
    		char array3[strlen(pword)+1];
    		n=malloc(sizeof(struct node));
    		p->word=malloc(strlen(pword)+1);
    		strcpy(p->word, pword);
    		strcpy(array1, pword);
    
    		int i;
    		int j=0;
    		for ( i = 0; array1[i] != '\0'; i++ )
    		{
    			if ( array1[i] != ',' || array1[i] != '.' || array1[i] != '(' || array1[i] != ')' )
    			{
    				array3[j]=array1[i];
    				j+=1;
    			}
    		}
    		
    		if(strcmp(array3, array2)==0)
    		{
    			save=1;
    		}
    		pword=strtok(NULL, " \n#");
    		p->next=n;
    		p=n;
    	}
    	return head; 
    }


    anyhow, is this part properly written? The purpose is to to delete the ,.() signs from array1.
    Code:
    		int i;
    		int j=0;
    		for ( i = 0; array1[i] != '\0'; i++ )
    		{
    			if ( array1[i] != ',' || array1[i] != '.' || array1[i] != '(' || array1[i] != ')' )
    			{
    				array3[j]=array1[i];
    				j+=1;
    			}
    		}
    I got the idea from this thread:
    Parsing and Tokens (strtok)
    Last edited by sababa.sababa; 02-15-2010 at 10:27 AM. Reason: Updated the post...

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    I made a separate function for checking the ,.() signs, but when askword is bigger than three letters, then save turns 0 even though wordtwo should be equal to wordask:

    Code:
    void theloop()
    {
    	char wordask[strlen(askWord)+1];
    	strcpy(wordask, askWord);
    	
    	char wordone[strlen(pword)+1];
    	strcpy(wordone, pword);
    	char wordtwo[strlen(pword)+1];
    	int i;
    	int j=0;
    	for ( i = 0; wordone[i] != '\0'; i++ )
    	{
    		
    		if ( wordone[i] == ',' || wordone[i] == '.' || wordone[i] == '(' || wordone[i] == ')' )
    		{
    		}
    
    		else
    		{
    			wordtwo[j]=wordone[i];
    			j++;
    		}
    
    	}
    	if(strcmp(wordtwo, wordask)==0)
    	{
    		save=1;
    	}
    
    }
    I suppose it has something to do with /0 characters.... any ideas?

    /n00b

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Hurray! It's working the way I want... I made a workaround, but I'm really currious why the loop in the previous post didn't work. Here's the new code:

    Code:
    void theloop()
    {
    	char wordask[strlen(askWord)+1];
    	strcpy(wordask, askWord);
    	
    	char wordone[strlen(pword)+1];
    	strcpy(wordone, pword);
    	char wordtwo[strlen(pword)+1];
    	int i;
    	int j=0;
    	for ( i = 0; i<(strlen(askWord)+1); i++ )
    	{
    
    		if ( wordone[i] == ',' || wordone[i] == '.' || wordone[i] == '(' || wordone[i] == ')' )
    		{
    		}
    
    		else
    		{
    			wordtwo[j]=wordone[i];
    			j++;
    		}
    
    	}
    	if(strcmp(wordtwo, wordask)==0)
    	{
    		save=1;
    	}
    }

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sababa.sababa View Post
    I'm really currious why the loop in the previous post didn't work.
    You never zero terminated wordtwo.
    Kurt

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks ZuK!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  2. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  3. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  4. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM