I would like to automatically generated staff number before generated the staff number i would like to check the staff.txt is the staff number exist. If exist then it will get the last's staff number and continue to adding. But the code that i creating it wont work. Please help to me rewrite.
void staff::setstaffid()
{
int count = 0;
ifstream instaffile("staff.txt", ios::in);
if(!instaffile)
{
ofstream outstaffile("staff.txt", ios::out);
outstaffile.close();
ifstream instaffile("staff.txt", ios::in);
}
instaffile.read(reinterpret_cast<char *>(this), sizeof(staff));
if(instaffile.eof()) //if the staff id is 0 then it will start from 20000
{
staffid = 1;
return;
}
else
{
while(instaffile && !instaffile.eof())
{
count++;
instaffile.read(reinterpret_cast<char *>(this),sizeof(staff));
}
}
staffid = 1 + (count); //increment the staff id
instaffile.close();
return;
}