Thread: Linked list problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Linked list problem

    After the second enter ,the program stop working,any idea what I did wrong,Ithink i have problem in the loop ,but I can't see it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef
        struct node{
        char name[20];
        int id;
        struct node *next;
       }student;
       void printList(student *ptr_head);
    int main()
    {   
        student *first,*current;
        char ime[20];
        int ID,i;
        printf("Enter name: ");
        scanf("%s",ime);
        printf("Enter id: ");
        scanf("%d",&ID);
        first=malloc(sizeof(student));
        strcpy(first->name,ime);
        first->id=ID;
        first->next=NULL;
        first=current;
        for (i=0;i<3;i++)
        {
          printf("Enter name: ");
          scanf("%s",ime);
          printf("Enter id: ");
          scanf("%d",&ID);
          student *temp;
          temp=malloc(sizeof(student));
          strcpy(temp->name,ime);
          temp->id=ID;
          temp->next=NULL;
          current->next=temp;
          current=temp;
        }   printList(first);
       getch();
       return 0;
    }
     void printList(student *first)
       {
        while(first){
           printf("%s : %d \n",first->name,first->id);
           first =first->next;
                       }
       }
    Last edited by lio; 11-24-2010 at 07:11 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    After you enter the integer, the newline character is still in the input buffer. Suck all the remaining characters out of the input buffer before you iterate. (getchar() until you reach the newline)
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks but ,I didn't get it ,could you show me where to change the code

    do I have to
    fflush(stdin);
    Last edited by lio; 11-24-2010 at 08:09 PM.

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    before accepting string

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by Dino View Post
    thanks a lot Dino for the link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List help!!
    By onepuzzledstud in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 08:36 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM