#include <stdio.h>
#define MAX_TITLE_SIZE 20
#define MAX_BOOKS 10
struct Book{
int _isbn;
float _price;
int _year;
char _title[MAX_TITLE_SIZE];
int _qty;
};
void displayInventory(const struct Book book[], const int size){
int i;
printf("===================================================\n");
printf("ISBN Title Year Price Quantity\n");
printf("---------+------------------+----+-------+--------\n");
//iterate through objects
if(size == 0){
printf("The inventory is empty!\n");
printf("===================================================");
}
else{
for(i = 0; i < size; i++){
printf("%-10.0d%-18s%5d $%-8.2f%-8d\n", book[i]._isbn, book[i]._title, book[i]._year, book[i]._price, book[i]._qty);
}
}
}
void addBook(struct Book book[], int *size){
if(*size == MAX_BOOKS){
printf("the inventory is full\n");
}
else{
//increment inventory size
(*size)++; //*size will move the pointer to the next in position/hashcode
//++*size also works
printf("ISBN:");
scanf("%d", book[*size]._isbn);
printf("Title:");
scanf("%s", book[*size]._title);
printf("Year:");
scanf("%d", book[*size]._year);
printf("Price:");
scanf("%f", book[*size]._price);
printf("Quantity:");
scanf("%d", book[*size]._qty);
printf("The book is sucessfully added to the inventory.\n");
printf("\n");
}
}
int main(void){
int size = 0;
struct Book book[MAX_BOOKS];
book[0]._isbn = 1234;
printf("Create string: ");
scanf("%s", book[0]._title);
book[0]._year = 1992;
book[0]._price = 192.90;
book[0]._qty = 2;
size++;
addBook(book, &size);
displayInventory(book, size);
return 0;
}
所以我有这个簿记系统,我试图制作,我想要做的是在book []数组结构中添加一本新书。
当我尝试在addBook()函数中输入书籍的ISBN时,整个应用程序以非零状态结束。
如何从函数写入数据结构?对不起,如果这是一个愚蠢的问题,但我已经坚持了一段时间。
我做错了什么?
这是指向repl.it的链接:https://repl.it/JRPH/4
答案 0 :(得分:1)
使用address
时,您应该参考scanf
变量。具体而言,scanf("%d", book[*size]._isbn);
- &gt; scanf("%d", &(book[*size]._isbn));
等等......
看看this。