在struct之前的错误预期表达式

问题描述 投票:0回答:1

阻止我的代码编译的错误是:

[错误]预期标识符或'(''''''令牌之前

[错误]'书籍'之前的预期表达

[错误]函数'fread'的参数太少

我是C编程和编程的新手。虽然我习惯了Pascal。除主菜单和void菜单外,所有函数都会出现错误。这只是我代码的一部分。

请有人帮我解决这个程序的错误。

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>                 
#include <ctype.h>                   

char catergories[][15]={"Computer","Philosophy","Arts","Literature","Science","History"};
void menu(void);
void addbooks(void);
void deletebooks(void);
void updatebooks(void);
void findbooks(void);
void sellbooks(void);
void viewbooks(void);
int  enterinfo();
int  checkid(int);
void Password();

//list of global files that can be accessed from anywhere in program
FILE *fp,*ft,*fs;

//list of global variable
int s;
char findbook;
char password[10]={"dominique"};

typedef struct
{
    int mm,dd,yy;
}OrderDate;

typedef struct
{
    int id;
    char name[35];
    char author[35];
    int quantity;
    float price;
    int count;
    int shelf;
    char *cat;
    struct OrderDate *sold;
}books;

void addbooks(void)    //funtion that add books
{
    system("cls");

    int i;
    struct books;

    printf("\n ******************************************************\n");
    printf("\n \t\t ADD A BOOK");
    printf("\n ******************************************************\n\n");
    printf("\n SELECT THE CATERGORY OF THE BOOK:");
    printf("\n 1. Computer Science, Information & General Works");
    printf("\n 2. Philosophy, Psychology and Religion");
    printf("\n 3. Arts and Recreation");
    printf("\n 4. Literature");
    printf("\n 5. Science");
    printf("\n 6. History and Geography");
    printf("\n 7. Back to main menu\n");
    printf("\n Enter your choice:");
    scanf("%d",&s);

    if(s==7)
    {
        menu() ;
    }

    system("cls");
    fp=fopen("BookRecords.txt","ab+");

    if(enterinfo()==1)
    {
        books.cat=catergories[s-1]; //the error that exists here states expected identifier or '(' before '.' token
        fseek(fp,0,SEEK_END);
        fwrite(&books,sizeof(books),1,fp); //the error that exists here states expected expression before 'books'
        fclose(fp);
        printf("\n The book's information has been saved sucessfully");
        printf("\n Would you like to save more information? (Y/N):");

        if(getch()=='n')
        {
            menu();
        }
    else
        system("cls");
        addbooks();
    }
}
c file struct syntax
1个回答
0
投票
struct books;

books是你的类型名称。像intchar。这就是为什么books.id==d不起作用。你必须声明作为书籍类型的变量。例如:

books BooksInMyShop;

现在您可以在代码中使用您的变量:

if(BooksInMyShop.id == currentId)

PS1:当您定义:

typedef struct
{
 (...)
}books;

你不需要通过struct books BooksInMyShop;声明变量简单的books BooksInMyShop;现在可以工作。

PS2:我可以在你的代码中看到许多错误观念。例如,在每个函数中的“变量”books不在它们之间共享。我想你必须将你的bookstore的一个实例传递给每个函数或者有一个静态全局实例。

© www.soinside.com 2019 - 2024. All rights reserved.