Thread: help me

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    3

    help me

    this asks for info about a book
    but it says that it cant find the header file
    Code:
    struct bookInfo {
        char title [40];
        char author [25];
        float price;
        int pages;
    };
    
    
    #include <bookinfo.h>
    #include <stdio.h>
    
    
    main()
    {
        int ctr;
        struct bookInfo books[3];
    
    
    
    
        for (ctr = 0; ctr < 3; ctr++)
        {
            printf("What is the name of the book #%d?\n\n", (ctr+1));
            gets (books[ctr] .title);
            puts("Who is the author?");
            gets(books[ctr].author)
            puts("How much did the book cost?");
            scnaf(" $%f", &books[ctr].price);
            puts("How many pages in the book?");
            scanf(" %d", &books[ctr].pages);
            getchar();
        }
    
    
    
    
    
    
        printf("\n\nHere is the collection of books: \n\n");
        for (ctr = 0; ctr < 3; ctr++)
        {
            printf("#%d: %s by %s",
                   (ctr+1), books[ctr].title, books[ctr].author);
            printf("\n\nIt is %d pages and costs $.2f",
                   books[ctr.pages, books[ctr].price);
            printf("\n\n");
        }
    
    
    
    
    return (0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,423
    Which header file?
    You already declared your struct, so bookinfo.h seems unnecessary.

    If your book is that old that it doesn't declare main properly, and thinks gets() is a good idea, you need to throw it away.

    Also, check your spelling of scanf.

    Also, "help me" is about the dumbest title you can choose.
    How To Ask Questions The Smart Way
    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
    Banned
    Join Date
    Aug 2017
    Posts
    861
    plus after what Salem said about that, if you where to put it into a file called bookinfo.h it is referenced like this,
    Code:
    #include "bookinfo.h"
    quotes are for local files, < > brackets are for system ones.

    and formating goes like this, if you actually did it like that in your main.
    Code:
    
    #include <stdio.h>
     
    
    #include "bookinfo.h"
    
    
    
    struct bookInfo {
    
        char title [40];
    
        char author [25];
        float price;
    
        int pages;
    
    };
    
    int main()
    {
    
    // stuff
    return 0;
    }
    Last edited by userxbw; 09-30-2017 at 04:32 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    books[i.pages, books[i].price]

    You meant to access books[i] right? But look where the bracket traveled. Keep an eye out for these types of small syntax errors. They plague everyone.

    Edit: a user by the name escapeSequence had posted code with this error but deleted his post in apparent shame. Sorry that he removed the context.
    Last edited by whiteflags; 09-30-2017 at 04:51 PM.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    1
    There were several syntax errors.

    Code:
    #include "bookinfo.h"
    #include <stdio.h>
     
     
    int main(void) {
        struct bookInfo books[3];
     
        for (int i = 0; i < 3; i++)
        {
            printf("What is the name of the book #%d?\n\n", (i+1));
            gets (books[i] .title);
            puts("Who is the author?");
            gets(books[i].author);
            puts("How much did the book cost?");
            scanf(" $%f", &books[i].price);
            puts("How many pages in the book?");
            scanf(" %d", &books[i].pages);
            getchar();
        }
     
        printf("\n\nHere is the collection of books: \n\n");
        for (int i = 0; i < 3; i++)
        {
            printf("#%d: %s by %s",
                   (i+1), books[i].title, books[i].author);
            printf("\n\nIt is %d pages and costs $.2f",
                   books[i].pages, books[i].price);
            printf("\n\n");
        }
    
    
    return (0);
    }
    book.h
    Code:
    struct bookInfo {
        char title [40];
        char author [25];
        float price;
        int pages;
    };

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code:
    gets(books[ctr] .title);
    man gets | grep ^BUGS --after-context=8 -
    BUGS
    Never use gets(). Because it is impossible to tell without knowing the
    data in advance how many characters gets() will read, and because
    gets() will continue to store characters past the end of the buffer, it
    is extremely dangerous to use. It has been used to break computer
    security. Use fgets() instead.


    For more information, see CWE-242 (aka "Use of Inherently Dangerous
    Function") at CWE -

    CWE-242: Use of Inherently Dangerous Function (2.11)

Popular pages Recent additions subscribe to a feed

Tags for this Thread