Thread: problem with fgets

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14

    Unhappy problem with fgets

    Code:
    #include<stdio.h>
    #include<conio.h>
    struct student
    {
        char name[5][100];
        int roll[5];
        int sn[5];
        int mark[5][5];
    }
    main()
    {
        struct student stu;
        int i,j, sum=0;
        for(i=0; i<3; i++)
        {
            printf("Serial No: ");
            scanf("%d", &stu.sn[i]);
            printf("Enter Name: ");
            fgets(stu.name[i],sizeof(stu.name),stdin);
            printf("Enter Roll No: ");
            scanf("%d", &stu.roll[i]);
            printf("Enter Marks of 5 Subjects:\n");
            for(j=0; j<5; j++)
                {
                    scanf("%d", &stu.mark[i][j]);
                }
                        printf("\n\n");
        }
        printf("\nPress Enter to Continue\n");
        getch();
        for(i=0; i<3; i++)
        {
            printf("\nSerial No.: %d", stu.sn[i]);
            printf("\nYour Name: %s", stu.name[i]);
            printf("\nYour Roll No: %d", stu.roll[i]);
            printf("\nYour Marks: ");
            for(j=0; j<5; j++)
            {
                printf("\n\t%d", stu.mark[i][j]);
                sum=sum+stu.mark[i][j];
            }
            printf("\nTotal Marks: %d", sum);
            sum=0;
            printf("\n\n");
            printf("Press Enter to Continue\n");
            getch();
        }
    }
    Somebody please help me why this program doesn't work properly? If i use scanf instead of fgets then it works fine but the problem with scanf is i cant store a string with space thats why i am using here fgets but after using fgets my program doesn't work. I am attaching my output Screenshot.
    Attached Images Attached Images problem with fgets-3e-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    scanf leaves trailing white space (including newlines) in the input stream.
    Unfortunately for you, a trailing newline is immediate success for a following fgets call.

    What you could do:
    1. use fgets() for everything, then sscanf on the resulting buffer (or use other str... functions)

    2. Flush the input stream between scanf and fgets calls - see FAQ > Flush the input buffer - Cprogramming.com

    3. Use scanf("%[^\n]",stu.name[i]), which will read all characters except a newline into your buffer.

    Choice 1 is usually the best way forward, especially when you have to start dealing with foolish (or worse, hostile) users.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Location
    India
    Posts
    14
    Thanks for quick reply. I will surly test option 1 meanwhile i rectify this problem by using getchar() function just after the first scanf() function and before fgets() function it simply ignore the extra characters left by scanf() and now codes are working properly but still i think that option no. 1 would be much better if i could implement this. below is part of new code where i used getchar function
    Code:
            printf("Serial No: ");
            scanf("%d", &stu.sn[i]);
            getchar();
            printf("Enter Name: ");
            fgets(stu.name[i],sizeof(stu.name),stdin);
            printf("Enter Roll No: ");
            scanf("%d", &stu.roll[i]);
    Last edited by raj21; 11-08-2015 at 12:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets problem
    By cfanatic in forum C Programming
    Replies: 27
    Last Post: 08-25-2012, 05:44 AM
  2. fgets() Problem
    By kihina in forum C Programming
    Replies: 3
    Last Post: 02-08-2010, 11:03 AM
  3. fgets problem
    By belhifet in forum C Programming
    Replies: 14
    Last Post: 11-29-2006, 05:07 PM
  4. fgets problem (i think)
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 11:19 AM
  5. fgets problem
    By ober in forum C Programming
    Replies: 5
    Last Post: 04-21-2002, 06:47 PM

Tags for this Thread