-2

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 -

  1. 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".
  2. 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"!!!
  3. "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

  • 2
    Well, did you step through the code in a debugger? You are overwriting some memory somewhere, or have a bad pointer. – OldProgrammer Dec 24 '15 at 16:08
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Dec 24 '15 at 16:10
  • What is user? If it's a class then you need to instantiate it in dis_leader_board(). – nicomp Dec 24 '15 at 16:12
  • 1
    No one can debug your program for you since you haven't provided a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve). – Carey Gregory Dec 24 '15 at 16:16
  • @nicomp What did you mean by instantiate?? – Siddharth Kailasam Dec 24 '15 at 16:17
  • @SiddharthKailasam You didn't answer my question. – nicomp Dec 24 '15 at 16:18
  • "instantiate" : create an instance (an object) of a class. –  Dec 24 '15 at 16:27
  • When you read a user from a file (why does your "display" function read?), you need to create some sort of instance of the user that's more permanent than you function argument. –  Dec 24 '15 at 16:36
  • I have now edited this page. I guess I have given as much information as I can.Whoever sees this please read the edited question and if you feel it is I deserve to get unheld please vote to re-open.If you don't think so, at least mention your concerns to me. – Siddharth Kailasam Dec 24 '15 at 17:55

1 Answers1

2

You need to show the definition of your user class to be certain, but I suspect that this class contains non-POD members.

As such, your approach to simply write and read instances of these classes to a file will, obviously, not work. You will be writing a bunch of raw pointer values, and upon reading them back these raw pointer values mean absolutely nothing, and accessing them is the most likely cause of your "general protection error"s.

This is the best that can be determined from the limited information provided in your question.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148