0
#include <stdio.h>
#include <string.h>

struct library_catalog
{
    char book_title[100] ;
    char author[100];
    char publisher[100];
    int edition;
    int YOP;
    float price;
};

void getdata(struct library_catalog A[ ], int);
void display(struct library_catalog A[ ], int);

int main()
{
    int n;

    printf("Enter the total number of books : \n");
    scanf("%d", &n);

    struct library_catalog books[n];

    getdata(books, n);
    display(books, n);

    return 0;
}


void getdata( struct library_catalog A[ ], int n)
{
    int i, size;
    char string[50];

    for (i = 0; i < n; i++)
    {   
        int j = i + 1;
        printf("\nFor Book %d            : ", j);

        printf("\nEnter the title of the book:");
        fgets(A[i].book_title, 100, stdin);
        size = strlen(A[i].book_title);
        A[i].book_title[size-1] = '\0';

        printf("\nEnter the author's name: ");
        fgets(A[i].author, 100, stdin);
        size = strlen(A[i].author);
        A[i].author[size-1] = '\0';

        printf("\nEnter the name of the publisher: ");
        fgets(A[i].publisher, 100, stdin);
        size = strlen(A[i].publisher);
        A[i].publisher[size-1] = '\0';

        printf("\nEnter the edition of the book: ");
        scanf("%d", &A[i].edition);

        printf("\nEnter the year of publishing: ");
        scanf("%d", &A[i].YOP);

        printf("\nEnter the price of book: ");
        scanf("%f", &A[i].price);

        size = 0;
    }

    return;

}


void display (struct library_catalog A[ ], int n)
{
    int i;

    for (i = 0; i < n; i++)
    {
        int j = i + 1;
        printf("\nBook %d            : ", j);
        printf("\n______________________________________________________________\n");
        printf("\nTitle              : %s", A[i].book_title);
        printf("\nAuthor             : %s", A[i].author);
        printf("\nPublisher          : %s", A[i].publisher);
        printf("\nEdition            : %d", A[i].edition);
        printf("\nYear of Publishing : %d", A[i].YOP);
        printf("\nPrice              : %.2f", A[i].price);
        printf("\n______________________________________________________________\n");
    }

    return;

}

After execution, the program does not read or scan (See the program output) the name of the book title. I guess it has to do with the use of too many string input command (fgets). Is it correct? How can I fix it?

Gaurav Pathak
  • 1,065
  • 11
  • 28
Shantan
  • 11
  • 4
  • The issue is when you enter number of books and press enter, the newline is getting as the input to immediate next `fgets` A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str. – Gaurav Pathak Apr 15 '17 at 14:10
  • A quick solution is to use `getchar()` immediately after `scanf("%d", &n);` at line 23. – Gaurav Pathak Apr 15 '17 at 14:18
  • Another solution which I found out later is to use the `fflush(stdin)` function at line 43. – Shantan Apr 24 '17 at 16:35
  • Flushing standard input is not good and is not recommended as it may cause unpredictable behavior during code execution. – Gaurav Pathak Apr 26 '17 at 10:23

0 Answers0