link list

This is a discussion on link list within the C Programming forums, part of the General Programming Boards category; Ladies and gents, im a newby to C and i'm trying to get this linked list to work... the programm ...

  1. #1
    nid
    Guest

    link list

    Ladies and gents,
    im a newby to C and i'm trying to get this linked list to work... the programm runs upto enter student name... once student name has been emtered the programme drops out...i have seen other posts of a similar sortof programme but non of them have this problem.
    Thanks
    nid
    Code:
    
    char Search (char);
    
    struct student                                     /*data structure for student records*/
            {
             char student_name[30];
             char deg_class[30];
             float second_year;
             float fourth_year;
             float pro_percent;
             float opt_percent;
             struct student *next;
            }*ListHead, *Previous, *Current;
         
    
    void InsertNode()
    {
            float Int; char Convert;
            struct student *NewNode;
    
            NewNode = (struct student *)malloc(sizeof(struct student));
            printf("\n Enter Student Name: ");  scanf("%s",NewNode->student_name); 
            printf("\n Enter Second Year Mark: ");  scanf("%f", NewNode->second_year); /* fault on this line*/
            printf("\n Enter Fourth Year Mark: ");  scanf("%f", NewNode->fourth_year); /* fault on this line*/
            printf("\n Enter Project Mark: ");  scanf("%f", NewNode->pro_percent);
            printf("\n Enter Option Mark: ");  scanf("%f", NewNode->opt_percent);
            NewNode->next = NULL;
            FindInsertPos(NewNode);
            NewNode->next = Current;
            Previous->next = NewNode;

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    [EDIT]
    A very similar source code has been posted before:
    http://cboard.cprogramming.com/showt...threadid=38977
    [/EDIT]

    You must empty the stdin buffer before each call to scanf(). Otherwise a '\n' might have been left in stdin.
    Code:
    void tidy(FILE *in)
    {
        int c;
        while((c = fgetc(in)) != EOF && c != '\n');
    }
    Call this function before scanf() to clear the input buffer.
    Last edited by Sargnagel; 05-05-2003 at 08:27 AM.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    7

    Talking clearing input buffer

    you can also use this to clear the input buffer instead of writing a function.

    you might need stdlib or stdio. i dont remember exactly.

    the function is included in the library.

    fflush(stdin); //will empty the input buffer.

    better and simpler than writing your own function (i think).

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    7
    i forgot one thing.

    suppose you have an open file input named finput

    fflush(finput); //would empty the input buffer in the file.

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    fflush(stdin);

    This is undefined by the ANSI C99 Standard! fflush() is only defined for output buffers!
    Have a look at the FAQ.

  6. #6
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >better and simpler than writing your own function
    Simpler sure, but most certainly not better since flushing input streams is highly implementation dependent. It works if your compiler allows it, otherwise you risk invoking undefined behavior. By the way, fflush is in stdio.h.

    A better alternative to sidestep all of this is to not use scanf to read strings in the first place.
    p.s. What the alphabet would look like without q and r.

  7. #7
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by vVv

    Less than perfect - Think about what happens if there's no newline at all.
    That's a point! But why won't it work? If stdin is empty, fgetc returns EOF or am I wrong?
    Hmmm ... I may be wrong, because - if I remember correctly - a few months ago I had some problems with calling this tidy function with no call to scanf before ... hmmm ...

    @source code examples:
    Very interesting stuff! Thank you for this "offtopic" stuff. I have added it to my archive of useful C code.

  8. #8
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    fgetc( ) will block and wait until new data is available - this is the default for all blocking descriptors
    Ok, now I've got it. Another fact to add to my archive.
    Thanks for explaining.

  9. #9
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    I think I do now understand this EOF concept. Thanks again for your enlightening postings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 10:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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