-1

I make the loop with gets and scanf. Actually I make the space for newline buffer. so I think each scanf works but when I input value to &books[ctr].cost compiler skip next 3 questions. I don't know why It work like this

books is structure variable

for (ctr = 0 ; ctr < 3 ; ctr++)
{
    printf("What is the name of the book #%d?\n", (ctr+1));
    gets(books[ctr].title);
    puts("Who is the author?  ");
    gets(books[ctr]. author);
    puts("How much did the book cost?  ");
    scanf("  $%f", &books[ctr].price);
    puts("How many pages in the book?  ");
    scanf("  %d", &books[ctr].pages);
    getchar( );
}
printf("\n\n Here is the collection of books : \n");
for (ctr = 0 ; ctr < 3 ; ctr ++)
{
    printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
    printf("\n It is %d pages and costs $%.2f", books[ctr].pages, books[ctr].price);
    printf("\n\n");
}

return 0;

}

1 Answers1

0

First of all, two suggestions:

Use fgets() to take user input, and then convert to appropriate form using sscanf().

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • but this is my college assignment and It's example from my major textbook. so It must work with that code. I don't know why It's not working – Jeon Soo min Nov 11 '20 at 08:09