变量未声明,即使它在C程序中也是如此



有一个错误表明book未声明,并且有一个注释表明"对于"中出现的每个函数,每个未声明的标识符只报告一次;。但我不明白为什么它只适用于book.title,而struct中的其他成员却不受影响。

#include<stdio.h>
#include<string.h>
struct LIS
{
char title[75];
char author[75];
char borrower_name[75];
int days_borrowed;
float fine;
};
struct book;
void main() {
int response;
do {
printf("Title of the book: ");
gets(book.title);
printf("Author(s) of the book: ");
gets(book.author);
printf("Name of borrower: ");
gets(book.borrower_name);
printf("Number of days borrowed: ");
scanf("%d", &book.days_borrowed);
if(book.days_borrowed > 3) { book.fine = 5.00 * (book.days_borrowed-3); }
else { book.fine = 0; }
printf("Fine (if applicable): %.2fn", book.fine);
printf("Enter any key continue/Enter 0 to end: ");
scanf("%dn", &response);
} while (response != 0);
}

您应该替换book定义的代码,如下所示:

struct LIS
{
char title[75];
char author[75];
char borrower_name[75];
int days_borrowed;
float fine;
} book;

或者像这样:

struct LIS
{
char title[75];
char author[75];
char borrower_name[75];
int days_borrowed;
float fine;
}; 
struct LIS book;

变量book需要一个结构类型定义。简单地写struct book;不会指定book是什么样的结构


此外,请注意,函数gets已从C标准中删除,因为它不安全。相反,你应该使用这样的东西:

fgets(book.title,sizeof(book.title),stdin);

最新更新