Class help

Martonx

Newcomer
Joined
Mar 23, 2007
Messages
4
Location
Szeged, Hungary
Hi Everyone,

I have a C++ program lesson, but i can't to do it. I have a binary file with structured datas.
I have to read the datas, and take into a class (structure array).

Here is my code:
Code:
#include <iostream>
#include <fstream>

using namespace std;

struct dolgozo
	{
	char nev[30];
	unsigned int kor;
	long int fizetes;
	};

class dolgoszt
{
private: 
	dolgozo *adatok;
	int db;
public: 
	dolgoszt(char nev[])
	{
		int i=0;
		db=0;
		dolgozo r;
		ifstream zh;
		zh.open(nev);
		if (zh.fail()) {cout<<"Error when open the file";exit(1);}
		zh.read((char*) &r, sizeof(dolgozo));
		while (!zh.eof())						
		{
			db++;
		}
		adatok = new dolgozo[db];
		zh.beg;
		zh.read((char*) &r, sizeof(dolgozo));
		while (!zh.eof())						
		{
			adatok[i]=r;
			i++;
			zh.read((char*) &r, sizeof(dolgozo));
		}
		zh.close();
		}
	~dolgoszt();

	void kiir()
	{
		cout<<adatok->nev<<"\t"<<adatok->fizetes<<"\t"<<adatok->kor<<endl;
	}
	
};


int main()
{
	char file[30]="c:\\dolgozo.dat";
	dolgoszt d(file);
	cin.get();
}

But i can't compile this, i have some fatal error:confused:
I think this part has the problem:
Code:
adatok = new dolgozo[db];
zh.beg;
zh.read((char*) &r, sizeof(dolgozo));
while (!zh.eof())						
{
	adatok[i]=r;
	i++;
	zh.read((char*) &r, sizeof(dolgozo));
}

I attached the binary file. Please help me. I am new in C++, i can programming in Visual Basic, but it is very hard to learn C++ :eek:
 

Attachments

Nothing against your educational institution of choice, but they should have started you on C++ to begin with...and not classes/db programming.

Anyway, what's the text of the error it's throwing?
 
Well, I'll tell you what I think, see if that helps...

First, the compile errors:

dolgoszt d(file);

What is the d? I don't think that's valid syntax in c++.
Should just be: dolgoszt(file);


and this one:

adatok = new dolgozo[db];


Im pretty sure, this line: dolgozo *adatok;
won't allow you to save multiple dolgozo structures into it. A pointer is a container for a certain size of object and you are defining a pointer to a memory space for 1 dolgozo object. In this case, you might as well use an array, since you instantiate it as an array (adatok = new dolgozo[db]; ).

So, the change would be "dolgozo *adatok;" to "dolgozo adatok[];".



And for the logic errors:

1. The dolgoszt function doesn't give any output on success, so I would assume you want to call the kiir function after the call to it in the main function.


2. I think that this is a never ending loop:

while (!zh.eof())
{
db++;
}


unless eof increments the file pointer....which I don't think it does.

From what I see, you are trying to get the number of dolgozo structures in the file...You need to move the line before the while loop (zh.read((char*) &r, sizeof(dolgozo)); ) into the loop, so that the end condition will be met.

That does seem very inefficient though, you might want to refactor that part.
 
Code mistakes

First, the compile errors:

dolgoszt d(file);

What is the d? I don't think that's valid syntax in c++.
Should just be: dolgoszt(file);

The syntax is valid C++. dolgoszt is a class and d is an instance of dolgoszt, and the parameter file is passed to the constructor.

and this one:

adatok = new dolgozo[db];


Im pretty sure, this line: dolgozo *adatok;
won't allow you to save multiple dolgozo structures into it. A pointer is a container for a certain size of object and you are defining a pointer to a memory space for 1 dolgozo object. In this case, you might as well use an array, since you instantiate it as an array (adatok = new dolgozo[db]; ).

So, the change would be "dolgozo *adatok;" to "dolgozo adatok[];".

Again, incorrect. Martonx has the correct syntax for declaring and initializing an array - although you may wish to implement a destructor so that the array can be deleted when you are finished with the object.

1. The dolgoszt function doesn't give any output on success, so I would assume you want to call the kiir function after the call to it in the main function.

dolgoszt is a class, not a function.

The rest of your post, about the never ending loop, is accurate.

Good luck :cool:
 
Woops, yeh, didn't pick up that that was a class with a constructor.

So, MrPaul, where is the compiler error then?
 
Back
Top