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

时间:2018-03-03 18:55:50

标签: c file struct syntax

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

  

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

     

[错误]预期表达式' books'

     

[错误]功能失败的参数太少' 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();
    }
}

1 个答案:

答案 0 :(得分:0)

struct books;

books是您的类型名称。与intchar一样。这就是为什么books.id==d无法工作的原因。你必须声明作为书籍类型的变量。例如:

books BooksInMyShop;

现在您可以在代码ex。:

中使用您的变量
if(BooksInMyShop.id == currentId)

PS1:当你定义:

typedef struct
{
 (...)
}books;

您不需要通过struct books BooksInMyShop;声明变量简单books BooksInMyShop;现在可以使用。

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