C ++错误 - 在'之前预期的主要表达式。'令牌|在结构中

时间:2014-04-17 09:58:20

标签: c++ structure

我只是想说我还在学习C ++所以我从关于Structures的模块开始,虽然我不了解所有内容,但我认为我有点对了。编译器给我的错误是:

错误:在'之前预期的primary-expression。'令牌|

#include <iostream>
#include <fstream>
using namespace std;

struct catalog
{
    char title[50];
    char author[50];
    char publisher[30];
    int yearpublish;
    double price;
};

int main()
{
    catalog book;
    char again;

    fstream fbook;
    fbook.open("book.dat", ios::out | ios::binary);

    do
    {
        //Read data about the book
        cout << "Enter the following data about a book:\n";
        cout << "Title:";
        cin.getline (book.title,50);
        cout << "Author:";
        cin.getline (book.author,50);
        cout << "Publisher name:";
        cin.getline (book.publisher,30);
        cout << "Year publish:";
        cin >> book.yearpublish;
        cin.ignore();
        cout << "Price:";
        cin >> book.price;
        cin.ignore();

        //write the contents to the binary file
        fbook.write((char*)&catalog , sizeof(book));

        cout << "Do you want to enter another record?(Y/N)";
        cin >> again;
        cin.ignore();
    } while (again == 'Y' || again == 'y');

    fbook.close();
    return 0;
}

请帮助。

1 个答案:

答案 0 :(得分:5)

你在这行做什么

fbook.write((char*)&catalog , sizeof(book));

我认为应该是

 fbook.write((char*)&book , sizeof(book));