I am working on a project in c++ where the records of various players are maintained in order of the points they have scored.Here are the segments of the code
1.Adding new user
void add_to_leader_board(user u)
{
fstream f("ldr brd.dat",ios::in|ios::binary);
fstream f2("temp.dat",ios::binary|ios::out);
user a;
int chk=0;
while(f.read((char*)&a,sizeof(user)))
{
if(a.getpts()<u.getpts()&&chk==0)
{
f2.write((char*)&u,sizeof(user));
chk++;
}
f2.write((char*)&a,sizeof(user));
}
f1.close();
f2.close();
remove("ldr brd.dat");
rename("temp.dat","ldr brd.dat");
dis_leader_board(u);
getch();
}
2.Displaying the records
void dis_leader_board(user u)
{
clrscr();
user a;
int i=0;
fstream f("ldr brd.dat",ios::in|ios::binary);
cout<<"Name";
gotoxy(15,1);
cout<<"Points";
while(f.read((char*)&a,sizeof(user)))
{
a.output(i); //The definition of this function has been written down
}
f.close();
}
3.Modifying the records of the existing user and insert in appropriate position (Written upto where the problem occurs)
void modify_leader_board(user u)
{
cout<<"Before saving ";
for(int i=0;i<5;i++)
{
delay(0,0,800);
cout<<". ";
}
delay(0,5);
clrscr();
dis_leader_board(u);
delay(1,10);
.
.//Problem occurs before this.So I don't think the remaining is reqd
.
4.Output function
inline void user::output(int r)
{
/* "name" and "pts"
are data members*/
r+=2;
gotoxy(1,r);
cout<<name;
gotoxy(15,r);
cout<<pts;
}
When I tried executing the program,the following were the problems I encountered -
- I invoked the function add_to_leader_board. It worked all fine. The name was added to the file. I was very happy. But after display of records, by invoking dis_leader_board(which is a part of add_to_leader_board itself), I got a message which said "General Protection Exception".
- I invoked modify_leader_board function. Again, it worked all fine until it reached the clrscr() function before invoking dis_leader_board. I was very happy ......But toozzz... This time even before it started to display records,I got a message saying "General Protection Exception"!!!
- "General Protection Exception" , "General Protection Exception" , "General Protection Exception", "General Protection Exception" ..... Wherever I go, it seems to follow me like the Hutch dog!!!
I have the feeling that even the over protection of minority ( whether required or not ) offered by any of the governments and organizations, would not match the protection the compiler offers to the code!!!!
When add_to_leader_board is called, exception (General Protection Exception) occurs at the statement f.close() (probably something to do with it being the last statement) in display_leader_board function (called by add_to_leader_board) and when modify_leader_board is called, it occurs even before calling the function display_leader_board
Could anyone help me find the error??
I also want to know all the other cases when general protection exception can occur.
I appreciate an early response.
Thank you