I am creating a minesweeper game on turbo c++ and have got that logical part spot on.
Now my aim is to save highscores by time, ascending order(max 10 records). The problem is that I have to use turbo c++ no matter what
To do so, I created a class player which has to members pname, time storing the obvious values. then if file doesn't exist then a new file is created and records are sent into it. No problem here.
The problem is in this part when the file already exists:
player test;
int flag=-1,count=0; //flag to mark position at which new record will be inserted and count to count total no. of records present
while(f.read((char*)&test,sizeof(test))&&count<=10) //file is already opened in ifstream mode
{
count++;
if(curr_p.time<=test.time&&flag==-1) //curr_p is the object passed into this fxn and contains the current player's time
{
flag=(f.tellg())/sizeof(test)-1;
}
}
f.close();
f.open("high.dat",ios::binary|ios::in|ios::out);
if(count<10&&flag==-1)
flag=count;
cout<<"\n flag"<<flag<<"\n";
if(flag!=-1)
{
cout<<"\n it's a highscore! now enter your name "<<endl;
gets(curr_p.pname);
for(int i=(count-1);i>=flag;i--)
{
if(i<10)
{
f.seekp((i)*sizeof(test),ios::beg);
f.read((char*)&test,sizeof(test));
f.write((char*)&test,sizeof(test));
}
}
f.seekp(flag*sizeof(test),ios::beg);
f.write((char*)&curr_p,sizeof(test));
}
f.close();
The problem here is that all is fine till there are 2 records, for example these are the contents after entering 2 records
Name Time
1. 2nd record 100
2. 1st record 200
after entering 3rd record-
Name Time
1. 3rd record 50
2. 1st record 200
3. 1st record 200
4. 2nd record 100
I can't figure out what's wrong here
EDIT1- Well thanks for the help everyone but I played around with my code a little more and made the number of entries to 11 and with some more changes, somehow, all started working fine. Guess I'm stuck with a 11 player highscore, but well it sounds cooler than a conventional 10 player one. I guess?