0

I have this program, its mainly the program from "C programming Absolute beginner's guide" on page 263 but I just added the header file info to the program under (over main()).

#include <stdio.h>

struct bookInfo {
    char title[40];
    char author[25];
    float price;
    int pages;

};

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));
        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(); //Clears last newline for next loop
    }

    // 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;
}

So the program does not function as intended because it:

  1. It shows the inputted cost as the number of pages.
  2. It shows 0 as the cost.
  3. It doesn't ask for the pages of the book.

I have tried to just change the cost and pages to see if it would help somewhat, but nothing changed which was also weird.

What is/are the problem/problems with this program?

Edit 1: Run input log:

What is the name of the book #1?
book1
Who is the author?
adam
How much did the book cost?
100
How many pages in the book?
What is the name of the book #2?
book2
Who is the author?
adam2
How much did the book cost?
102
How many pages in the book?
What is the name of the book #3?
book3
Who is the author?
adam3
How much did the book cost?
103
How many pages in the book?


Here is the collection of books:
#1: book1 by adam
It is 100 pages and costs $0.00

#2: book2 by adam2
It is 102 pages and costs $0.00

#3: book3 by adam3
It is 103 pages and costs $0.00


Process returned 0 (0x0)   execution time : 16.934 s
Press ENTER to continue.




  • 1
    Please show your **exact** input and resulting output. That is, show your full run log. I suspect your input is wrong. – kaylum Apr 09 '21 at 20:45
  • 4
    Oof, using `gets()` is dangerous. – Rafael Apr 09 '21 at 20:46
  • 2
    Always check the return value of `scanf` so you know if there was an error. Is there a reason you have a leading space in your format strings? – Retired Ninja Apr 09 '21 at 20:51
  • Changing the `gets` calls to `fgets(books[ctr].title, 40, stdin);` and `fgets(books[ctr].author, 25, stdin);` seems to fix the code. – Adrian Mole Apr 09 '21 at 20:52
  • Does this answer your question? [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Adrian Mole Apr 09 '21 at 20:53
  • 2
    @Methodius5510 This call of scanf scanf(" $%f", &books[ctr].price); requires to type the symbol '$' before the value of the price. – Vlad from Moscow Apr 09 '21 at 20:54
  • @Vlad Indeed. I tried OP's code (adding the $ symbol) but it still failed to ask for the very last input. Then I changed to using `fgets` and it worked. Not sure if that's worth an answer (or yours maybe) but the suggested duplicate covers the `gets` issue very well. – Adrian Mole Apr 09 '21 at 20:57
  • Thanks for learning me about gets being old and bad. However, I just removed that $ sign from this line "scanf(" $%f", &books[ctr].price);" and now it works as intended. Thanks Vlad. – Methodius5510 Apr 09 '21 at 21:05

0 Answers0