Thread: What is wrong with my while loop?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    What is wrong with my while loop?

    Hey guys,
    This is what I want to do..I want to traverse my linked list (thats why I use pWalker) and check and see if the user inputted last name matches that of contacts in the linked list (thats why I use strcmp)..
    if it does, I print that node
    if it does not exist, I print the error msg..
    now..how do I get it to work so that if it does find it, it does not print the contact details (or the error msg for that matter) over and over and over again??
    Any ideas, corrections?
    Thanks
    A

    Code:
    fflush(stdin);
    printf("Please enter last name: ");
    scanf("%s", lastName);
    fflush(stdin);
    pWalker = pList;
    while(pWalker)
     {
     if(strcmp(lastName, pWalker->directory.k.lName) == 0)
    {
     printNode(pWalker);
    }
    else
    {
     errorMsg();
    }
    }
    pWalker = pWalker->next;
    break;

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You are not traversing the list.

    Code:
    while(pWalker)
    {
        if(strcmp(lastName, pWalker->directory.k.lName) == 0)
        {
            printNode(pWalker);
        }
        else
        {
             errorMsg();
        }
    }
    pWalker = pWalker->next;
    You have an endless loop, since if pWalker isn't NULL, then the loop wil go on and on, since pWalker will never get NULL. To solve this, you must add the line in bold within the loop.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    thanks Shiro..it works..

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fflush(stdin);
    This is a big no-no since fflushing an input stream is very well undefined.

    It seems to me that this would work a bit better if you actually traverse the entire list, comparing each node and only print the error if you don't find the key:
    Code:
    printf("Please enter last name: ");
    scanf("%s", lastName);
    while ( getchar() != '\n' );
    pWalker = pList;
    while(pWalker) 
    {
      if(strcmp(lastName, pWalker->directory.k.lName) == 0) 
      {
        printNode(pWalker);
      }
      pWalker = pWalker->next;
    }
    errorMsg();
    break;
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. Whats wrong w/ my loop?? Please help?
    By aspand in forum C Programming
    Replies: 6
    Last Post: 05-30-2002, 03:42 AM