C ++学校项目帮助! ! [英] C++ school project help! !

查看:52
本文介绍了C ++学校项目帮助! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们!!



我输入了一个函数,但它显示声明错误。



这只是我输入的程序的片段..

请假设所有头文件和类声明都已包含在内..



我试图写出void club :: writeinto(),但错误仍然存​​在..



我尝试过:



这里是函数:

Hi friends!!

I have typed in a function, but it shows declaration error.

This is just the fragment of the program i have typed in..
Kindly assume that all header files and class declarations have been included..

I have tried to write void club:: writeinto() but the error still persists..

What I have tried:

here is the function:

void writeinto()
{
    club c;
    int N,i;
    {
        ofstream fout("project.dat",ios::binary);
        {
            if(!fout)
            {
                cout<<"Error";
                exit(0);
            }
            while(!fout.eof())
            {
                cout<<"Enter the number of records to be entered";
                cin>>N;
                {
                    for(i=0;i<N;i++)
                    c.enterdata();
                    fout.write((char*)&c,sizeof(c));
                }
                fout.close();
            }
        }
    }
}

推荐答案

我编辑了你的问题并搬了代码我试过的部分,并添加了一些丢失或不存在的缩进。最后似乎缺少一个大括号。另外,其他一些支撑是不必要的。



这是一个修改版本,删除了一些支撑并调整了间距。它还返回一个布尔变量来指示它是否有效。



顺便说一句,我从未编写过代码来检查写作时的EOF。除非你有一个永远不会增长的固定长度文件,否则我认为它永远不会发生。出于这个原因,我已将其评论出来。
I edited your question and moved the code to the "what I've tried" section and added some indentation that was either lost or non-existent. It seems there is a missing curly brace at the end. Also, some of the the other braces are unnecessary.

Here is a revised version with some of the braces eliminated and the spacing adjusted. It also returns a boolean variable to indicate whether it worked or not.

By the way, I have never written code that checks for EOF on writing. Unless you have a fixed length file that can never grow I don't think it will ever happen. For this reason I have commented it out.
bool writeinto()
{
    club c;
    int N,i;

    ofstream fout( "project.dat", ios::binary );
    if( ! fout )
    {
        cout<<"Error";
        return false;
    }
//  while( ! fout.eof() )
    {
        cout << "Enter the number of records to be entered";
        cin >> N;
        for( i = 0; i < N; i++ )
        {
           c.enterdata();
           fout.write( (char*)&c, sizeof(c) );
        }
        fout.close();
    }
    return true;
}


非常感谢Rick ..但我的编译器在bool声明中显示声明语法错误。我在学校没有做bool,所以我不知道..或者我的编译器有问题吗?
Thanks a lot Rick.. But my compiler shows declaration syntax error in the bool declaration. I have not done bool at school, so i have no idea about it.. Or is the problem with my compiler?


这篇关于C ++学校项目帮助! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆