Thread: printing with linked list problem

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    10

    printing with linked list problem

    Any help on this print function using a linked list. I am getting the print statement coming up twice.


    Code:
    #include "mystuff.h"                                                                             
    
    void countchar (NODE* pList)
     
    {
     // local declarations
      int a;
      int w = w;
      int sum = 0;
      NODE* pWalker;
      pWalker = pList;
      int testEOF;
        
     // functions
     while (testEOF != EOF)
     { printf("Enter a char to be counted for in the linked list: <EOF> to stop ");
       testEOF = scanf("%c", &a);
       while (pWalker)
       {
        w = (pWalker->data.key);
        if (w == a)
        sum++;
        pWalker = pWalker->link;
       }
      printf("The number of %c's in the linked list is: %d\n", a, sum);
    
     }
      return;
    }
    The output: I entered an 'a' and it printed the print statement and then I hit ctrl+d and it printed again. Not sure what Is going on.


    Code:
    [n00611323@osprey a7]$ ./a.out data
    b. The Linked list of chars reversed is: t d i m h c S n o r e m a C 
    c. The total number of vowels in the linked list is: 4
    Enter a char to be counted for in the linked list: <EOF> to stop a
    The number of a's in the linked list is: 1
    Enter a char to be counted for in the linked list: <EOF> to stop The number of 
    's in the linked list is: 1
    Enter a char to be counted for in the linked list: <EOF> to stop The number of 
    's in the linked list is: 1

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After the EOF is entered, you need a break from the loop, immediately.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I added the break but the code still gives an odd output

    Code:
    #include "mystuff.h"
    
    void countchar (NODE* pList)
    
    {
     // local declarations
      int a;
      int w = w;
      int sum = 0;
      NODE* pWalker;
      pWalker = pList;
      int testEOF;
    
     // functions
     while(testEOF != EOF)
     { printf("Enter a char to be counted for in the linked list: <EOF> to stop ");
       testEOF = scanf("%c", &a);
       if (testEOF == EOF)
       break;
       while (pWalker)
       {
        w = (pWalker->data.key);
        if (w == a)
        sum++;
        pWalker = pWalker->link;
       }
      printf("The number of %c's in the linked list is: %d\n", a, sum);
    
     }
      return;
    }

    Output with 'o' entered and then ctrl+d for EOF

    Code:
    [n00611323@osprey a7]$ ./a.out data
    b. The Linked list of chars reversed is: t d i m h c S n o r e m a C 
    c. The total number of vowels in the linked list is: 4
    Enter a char to be counted for in the linked list: <EOF> to stop o
    The number of o's in the linked list is: 1
    Enter a char to be counted for in the linked list: <EOF> to stop The number of 
    's in the linked list is: 1
    Enter a char to be counted for in the linked list: <EOF> to stop [n00611323@osprey a7]$

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I should have put this in the first post but this is the purpose of the function.

    It is supposed to read an input from the user and then scan the linked list for it and it will print the number of times that value is in the linked list. It will repeat this any number of times until the user enters ctrl-d, EOF.

    "
    Prior to each running of function d (until the user enters ctrl-d), the program should
    continuously prompt the user for the particular char
    to be counted. "

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
     while(testEOF != EOF)
     { printf("Enter a char to be counted for in the linked list: <EOF> to stop ");
       testEOF = scanf("%c", &a);
       if (testEOF == EOF)
       break;
       while (pWalker)
       {
        w = (pWalker->data.key);
        if (w == a)
        sum++;
        pWalker = pWalker->link;
       }
      printf("The number of %c's in the linked list is: %d\n", a, sum);
    
     }
    Can be written simpler
    Code:
    while (scanf("%c", &a) != EOF)
    {
       ....
    }
    and you really should initialize testEOF. Don't EVER read undefined variables (except if they are global). Because it might have the value of EOF and the while loop won't be executed.

    You are leaving the newline '\n' in the input, scanf() doesn't read it. So you have

    stdint = 'o' '\n'

    when you do scanf() you read 'o'. Everything is ok. Now you press CTRL+D. You flush the '\n' input, which means that scanf reads it (it is like pressing enter without entering a newline).
    So you get "the number of '\n' 's is " where '\n' just changes the line.
    When you press again CTRL+D then it does what you want. It has nothing so scanf() returns EOF. But you will print the message again since it is before scanf(). Which is OK.

    To remove remaining '\n' you can use fgets() or simply getc().

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I tried this but its not working right.


    Code:
    #include "mystuff.h"
    
    void countchar (NODE* pList)
    {
    // local declarations
    int a;
    int w = w;
    int sum = 0;
    NODE* pWalker;
    pWalker = pList;
    
    
    // functions
    printf("Enter a char to be counted for in the linked list: <EOF> to stop\n");
    while ((a = getc(stdin)) != EOF)
    {
    pWalker = pList;
    sum = 0;
    while (pWalker)
    {
    w = (pWalker->data.key);
    if (w == a)
    sum++;
    pWalker = pWalker->link;
    }
    printf("The number of %c's in the linked list is: %d\n", a, sum);
    printf("Enter a char to be counted for in the linked list: <EOF> to stop");
    }
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What does "not working right" mean?

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

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    C_ntua is saying to use getc or fgets to remove trailing newline.
    Code:
    while ((a = getc(stdin)) != EOF)
    {
     pWalker = pList;
     sum = 0;
     ...
     while( getc(stdin) != '\n' )              /* remove trailing newline */
         ;
     ... 
    }
    Last edited by Bayint Naung; 07-13-2010 at 07:10 AM.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I see. I ended up just adding a second char in my scanf statement so it would pick up the return character so now its basically discarded and is not search for or used in my printf statement.

    Before hand it took the return char and tried to search for it finding nothing and it would print it found nothing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM