qq_22752755 2015-07-08 02:46 采纳率: 0%
浏览 7213

C++出现 unexpected end of file found

下面是代码:

 #define StrNum 20
#define Type 20
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>        //输入输出文件流
using namespace std;
const int Maxb=100;       //最多可存储的图书


class Book
{
private:
    int lab;               //删除标记
    int num;               //图书编号
    int add;               //增加标记
    char number[StrNum];    //编号
    char bookname[StrNum];  //书名
    char bookkind[Type];    //图书类型
public:
    Book(){}
    char getkind()                 //获取图书类型
    {
        return bookkind[Type];
    }
    char getname()                 //获取书名
    {
        return  bookname[StrNum];
    }
    int getnum()                     //获取图书编号
    {
        return num;
    }
    int getlab()                    //获取删除标记
    {
        return lab;
    }
    void setname(char na[])         //设置书名
    {
        strcpy(bookname,na);
    }
    void setkind(char kin[])        //设置类型
    {
        strcpy(bookkind,kin);
    }
    void delbook()                  //删除图书
    {
        lab=1;
    }
    void addbook(int n,char *na,char *kin)    //增加图书
    {
        lab=0;
        num=n;
        strcpy(bookname,na);
        strcpy(bookkind,kin);
    }
    void disp()                     //输出图书
    {
        cout.flags (ios::left);     //设置对齐的标志位为左
        cout<<setw(15)<<num<<setw(20)<<bookname<<setw(15)<<bookkind<<endl;
    }
};

class BDatabase
{
private:
    int top;
    Book book[Maxb];                //图书记录
public:
    BDatabase()                     //构造函数,将book.txt读到book[]中
    {
        Book b;
        top=-1;
        fstream file("book.txt",ios::in);
        while (1)
        {
            file.read((char *)&b,sizeof(b));
            if (!file)
                break;
            top++;
            book[top]=b;
        }
        file.close();
    }
    void clear()                    //全删
    {
        top=-1;
    }
    int addbook(int n,char *na,char *kin)     //增加图书
    {
        Book *p=query(n);
        if(NULL==p)
        {
            top++;
            book[top].addbook(n,na,kin);
            return 1;
        }
        return 0;
    }
    Book *query(int bookid)          //查找图书
    {
        for(int i=0;i<=top;i++)
            if(book[i].getnum()==bookid&&book[i].getlab()==0)
            {
                return &book[i];
            }
            return NULL;
    }

    void disp()            //图书库
    {
        for(int i=0;i<=top;i++)
            if (book[i].getlab()==0)
                book[i].disp();
    }
    ~BDatabase()           //析构函数,将book[]写到book.txt中,实现文本的写入
    {
        fastream file("book.txt",ios::out);
        for(int i=0;i<=top;i++)
            if(book[i].getlab()==0)
                file.write((char*)&book[i],sizeof(book[i]));
            file.close();
    }
    void main()
    {
        BDatabase BookDB;
        char choice;
        char bname[40];
        char bkind[40];
        char newType[20];
        int bookid;
        Book *b;
        while (choice!='0')
        {
            cout<<"\t******************************************************************\n";
            cout<<"                                           \n\n";
            cout<<"\t\t\t图  书  管  理  系  统\n\n";
            cout<<"\t\t\t1    新增图书记录\n";
            cout<<"\t\t\t2    更改图书记录\n";
            cout<<"\t\t\t3    删除图书记录\n";
            cout<<"\t\t\t4    查找图书记录\n";
            cout<<"\t\t\t5    显示图书记录\n";
            cout<<"\t\t\t6    清空全部记录(慎用)\n";
            cout<<"\t\t\t0    退出系统\n";
            cout<<"                                           \n\n";
            cout<<"\t******************************************************************\n";
                cout<<"请选择(1,2,3,4,5,6,0):";
            cin>>choice;
            switch (choice)
            {
            case'1':
                cout<<"输入图书编号:"<<endl;
                cin>>bookid;
                cout<<"输入图书类型:"<<endl;
                cin>>newType;
                cout<<"输入图书书名:"<<endl;
                cin>>bname;
                BookDB.addbook(bookid,bname,newType);
                break;
            case'2':
                cout<<"输入图书编号:"<<endl;
                cin>>bookid;
                b=BookDB.query(bookid);
                if(b==NULL)
                {
                    cout<<"该图书不存在"<<endl;
                    break;
                }
                    cout<<"输入新的书名:"<<endl;
                cin>>bname;
                b->setname(bname);
                cout<<"输入新的类型:"<<endl;
                cin>>bkind;
                b->setkind(bkind);
                break;
            case'3':
                cout<<"读入图书编号:"<<endl;
                cin>>bookid;
                b=BookDB.query(bookid);
                if(b==NULL)
                {
                    cout<<"该图书不存在"<<endl;
                    break;
                }
                char tg1;
                cout<<"确定要删除此记录吗?(Y/N)"<<endl;
                cin>>tg1;
                if(tg1!='n'&&tg1!='N')
                {
                    b->delbook();
                    cout<<"已成功删除该书记录!"<<endl;
                    break;
                }
            case '4':
                cout<<"读入图书编号:"<<endl;
                cin>>bookid;
                b=BookDB.query(bookid);
                if(b==NULL)
                {
                    cout<<"该图书不存在"<<endl;
                    break;
                }
                cout<<"图书编号      图书名称            图书类型          \n";
                b->disp();
                break;
            case '5':
                cout<<"图书编号      图书名称            图书类型          \n";
                b->disp();
                break;
            case '6':
                char tg2;
                cout<<"确定要清空所有记录吗?(Y/N)"<<endl;
                cin>>tg2;
                if(tg2!='n'&&tg2!='N')
                {
                    BookDB.clear();
                    cout<<"已成功清空记录!"<<endl;
                    break;
                }
            case '0':
                break;
            default:cout<<"输入错误,请从新输入(数字为0~6):";
            }
        }
    }
  • 写回答

5条回答 默认 最新

  • 拼名三郎 2015-07-08 03:00
    关注

    BDatabase类定义没有分号

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大