0

i have a problem with the adding an data in array in structure. After typing the ISBN number it shows me an error "Segmentation Fault" and it closes down, i'm pretty sure there is something wrong with the scanf but i'm not quite sure why. Here's my code:

#include <stdio.h>
#define MAX_TITTLE_SIZE 20
#define MAX_BOOKS 10
struct Book{
        int _isbn;
        float _price;
        int _year;
        char _title[MAX_TITTLE_SIZE];
        int _qty;
};
int main(void){
        struct Book book[MAX_BOOKS];
        int size=0;
        int opt;
        printf("Welcome to the Book Store\n");
        printf("========================\n");
        while(1){
                menu();
                printf("\n");
                printf("Select: ");
                scanf("%d", &opt);
                if(opt == 1){
                        displayInventory();
                }
                if(opt == 2){
                        addBook();
                }
                if(opt > 3){
                        printf("Invalid input, try again:\n");
                }
                if(opt == 0){
                        printf("Goodbye!\n");
                        return 0;
                }
        }
}
void menu(){
        printf("Please select from the following options:\n");
        printf("1) Display the inventory.\n");
        printf("2) Add a book to the inventory.\n");
        printf("3) Check price.\n");
        printf("0) Exit.\n");
}
void displayInventory(const struct Book book[MAX_BOOKS], const int size){
        if(size >= 1){
                printf("\n");
                printf("Inventory\n");
                printf("====================================================\n");
                printf("ISBN      Title               Year Price  Quantity \n");
                printf("---------+-------------------+----+-------+--------\n");
                printf("%-10.0d%-20s%-5d$%-8.2f%-8d", &book->_isbn, &book->_title, &book->_year, &book->_price, &book->_qty);
        } else {
                 printf("====================================================\n");
                 printf("The inventory is empty!\n");
                 printf("\n");
        }
}
void addBook(struct Book book[MAX_BOOKS], int *size){
        if(*size== MAX_BOOKS){
                printf("The inventory is full\n");
        }
        if(*size < MAX_BOOKS){
                printf("ISBN:");
                scanf("%d", book->_isbn);
                printf("Title:");
                scanf("%c", book->_title);
                printf("Year:");
                scanf("%d", book->_year);
                printf("Price:");
                scanf("%f", book->_price);
                printf("Quantity:");
                scanf("%d", book->_qty);
                printf("The book is successfully added to the inventory.\n");
        }
}
  • 1
    Don't you get compiler warnings or errors about `displayInventory` and `addBook` not being declared? – aschepler Mar 28 '17 at 01:01
  • 1
    1) `addBook();` --> `addBook(book, &size);` – BLUEPIXY Mar 28 '17 at 01:02
  • 2
    2) `scanf("%d", book->_isbn);` --> `scanf("%d", &book[*size]._isbn);`...then `++*size;`. – BLUEPIXY Mar 28 '17 at 01:03
  • 1
    You also need to learn how to use `printf` and `scanf` properly. It seems you mix the arguments to these (passing pointers to `printf` and (non-pointer) values to `scanf`). You also need to understand how to do forward declarations, and pass arguments to functions. *And* how to use arrays. Perhaps you should take a few steps back, [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start over? – Some programmer dude Mar 28 '17 at 01:05
  • 1
    Also The [ISBN](https://en.wikipedia.org/wiki/International_Standard_Book_Number) code is insufficient for `int` (32 bits). – BLUEPIXY Mar 28 '17 at 01:07
  • 1
    Like mentioned above, `scanf` takes addresses as arguments. – albusshin Mar 28 '17 at 01:42

0 Answers0