I've tried searching but can't find the answer I'm looking for. The program works the way it should until it asks "How many pages in the book?", it then skips the scanf(input). New to C programming. Any help?
Code:
#include <stdio.h>
#include "bookinfo.h"
int main() {
int ctr;
struct bookInfo books[3]; // Array of three structure variables
// Get the information about each book from the user
for (ctr = 0; ctr < 3; ctr++) {
printf("What is the name of the book #%d?\n", (ctr+1));
fgets(books[ctr].title, sizeof(books), stdin);
puts("Who is the author? ");
fgets(books[ctr].author, sizeof(books), stdin);
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(); // Clears last newline for next loops
}
// Print a header line and then loop through and print the info
printf("\n\nHere 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("\nIt is %d pages and costs $%.2f", books[ctr].pages, books[ctr].price);
printf("\n\n");
}
return 0;
}
header file:
Code:
// Header File for Chapter 27, Program A
struct bookInfo {
char title[40];
char author[25];
float price;
int pages;
};