Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:
C++
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();
            }
        }
    }
}
Posted
Updated 21-Sep-18 17:41pm
v2
Comments
jeron1 21-Sep-18 14:18pm    
What is the error?
Richard MacCutchan 22-Sep-18 3:51am    
Where is the definition of club? Please show the code that is related to the problem, and the exact error message.

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.
C++
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;
}
 
Share this answer
 
v2
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?
 
Share this answer
 
Comments
Dave Kreskowiak 21-Sep-18 23:51pm    
So, change it to void and remove the return statement at the end.
Rick York 22-Sep-18 16:20pm    
I have no idea why a c++ compiler would not accept a bool type for a function. It really isn't necessary so try what Dave recommended.
Richard Deeming 25-Sep-18 13:03pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution. DO NOT post your reply as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900