我为书店程序编写了一些代码,其中包含一个函数,用于扫描输入的每本书的详细信息。但是,我得到了一个警告(34:4),这个警告意味着要通过这本书的标题。我假设它与标题是唯一包含字符的变量有关,但我不确定为什么这是一个问题。这是我的代码:
#include <stdio.h>
#define MAX_BOOKS 10 //Maximum number of books that exist in an inventory
#define MAX_TITLE_SIZE 20//Maximum number of characters in a book title
struct Book { //Declare struct Book
int _isbn; //International Standard Book Number
float _price; //Book price
int _year; //Publication year
char _title[MAX_TITLE_SIZE]; //Book title
int _qty; //Book Quantity
};
int menu();
void displayInventory(const struct Book book[], const int size);
void searchInventory(const struct Book book[], const int isbn, const int size);
void addBook(struct Book book[], int *size);
void checkPrice(const struct Book book[], const int size);
/* main program */
int main(void) {
struct Book book[MAX_BOOKS]; //An array of Book representing the inventory
int size = 0; //Number of books in the inventory. The inventory is initially empty
//Display a welcome message:
printf("Welcome to the book store\n");
printf("=========================\n");
int select = menu();
switch (select)
{
case 0: //Exit the program
displayInventory(book, &size);
break;
case 1: //Display the inventory
break;
case 2: //Add a book to the inventory
addBook(book, &size);
break;
case 3: //Check price
break;
default:
printf("Invalid input, try again\n");
break;
}
return 0;
}
int menu()
{
int option = 0;
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");
printf("Select ");
scanf("%d", &option);
return option;
}
void displayInventory(const struct Book book[], const int size)
{
int i = 0;
if (size > 0) {
printf("===================================================\n");
printf("ISBN Title Year Price Quantity\n");
printf("---------+-------------------+----+-------+--------\n");
for (i = 0; i < size; i ++) {
printf("%-10d%-20s%-5d$%-8.2f%-8d", book[i]._isbn, book[i]._title, book[i]._year, book[i]._price, book[i]._qty);
}
}
else {
printf("The inventory is empty!");
}
}
void searchInventory(const struct Book book[], const int isbn, const int size)
{
printf("Not implemented");
return;
}
void addBook(struct Book book[], int *size)
{
if(*size== MAX_BOOKS) {
printf("the inventory is full.");
}
else {
printf("ISBN\n");
scanf("%d", &book[*size]._isbn);
printf("Title\n");
scanf("%c", &book[*size]._title);
printf("Price\n");
scanf("%lf", &book[*size]._price);
printf("Year\n");
scanf("%d", &book[*size]._year);
printf("Qty\n");
scanf("%d", &book[*size]._qty);
printf("Book successfully added to inventory\n");
*size++;
}
}
void checkPrice(const struct Book book[], const int size)
{
printf("Not implemented");
return;
}