Recent

Author Topic: need help with calender  (Read 49694 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #75 on: September 23, 2017, 11:42:44 am »
Code: Pascal  [Select][+][-]
  1. procedure Tform1.AddRank(Namee: String; Score: Integer);
  2. begin
  3.  x := 1;
  4.  while (x <= 10) and (rank[x].score >= score) do inc(x);
  5.  if x <= 10 then // are we still below 10?, so we broke out the previous loop with score > rank[x].score
  6.   begin
  7.   for i:= 10 downto x+1 do
  8.     begin
  9.      rank[i].score:=rank[i-1].score;
  10.      rank[i].name:=rank[i-1].name;
  11.     end;
  12.   end;
  13.   rank[x].name:=name;
  14.   rank[x].score:=score;
  15.   reset(highscorefile);
  16.   seek(highscorefile, filesize(highscorefile));
  17.   write(highscorefile, info);
  18.   closefile(highscorefile);
  19. end;                    

like that?

also remember how you told me to read the file oncreate and write the file onclose?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   read(highscorefile, info);
  4.  
  5.   edit1.clear;
  6.   edit2.clear;
  7.   assignfile(highscorefile, 'highhscore.dat');
  8.   if not fileexists('highhscore.dat') then
  9.   begin
  10.     rewrite(highscorefile);
  11.     closefile(highscorefile);
  12.   end;
  13. end;          

i did this and run error103 message came up

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: need help with calender
« Reply #76 on: September 23, 2017, 12:00:09 pm »
You have a read on the file in FormCreate before you even opened the file.

For now first forget about reading and writing (just remove that first line in FormCreate) and see of the rest of the code works.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #77 on: September 23, 2017, 12:15:45 pm »
yupp everything works perfect, but when i close it and compile it again nothing is on the scoreboard

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #78 on: September 23, 2017, 12:17:42 pm »
also the score is all 0 but i just want them to be invisible

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #79 on: September 23, 2017, 12:19:45 pm »
oh and also when click the button the name becomes Form1 instead of the on i typed.

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: need help with calender
« Reply #80 on: September 23, 2017, 12:23:08 pm »
Ok.

You have a "if not fileexists('highhscore.dat') then"
You need to add the same (or use else) for "if fileexists".

In that case you just open the file and loop through the file with an "while not EOF" (you learned that earlier) in that other topic and fill in rank until you get to 10.

At FormClose you do the same but for writing.

(The reason you need to do a while loop in the beginning is so you make sure when you have less then 10 records in the file, you don't crash. Writing only 10 records shouldn't be a problem because you don't care about anything other than the first 10. Or do you?)

also the score is all 0 but i just want them to be invisible
Just don't fill the StringGrid if the value is 0 for that rank.

oh and also when click the button the name becomes Form1 instead of the on i typed.
That's because you didn't use the parameter Namee to fill the new score-rank but the variable Name (which is the caption of the form).

Now you also see it is best not to use variable which can be confused with properties of another component (like the form). So Name, Width, Top, Left are all names of your own-variables you need to avoid.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #81 on: September 23, 2017, 12:41:32 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   if fileexists('highscore.dat') then
  4.   begin
  5.    while not eof(highscorefile) do
  6.     begin
  7.     for i:=1 to 10 do
  8.     write(highscorefile, rank[i]);
  9.     end;
  10.   end;
  11. end;
  12.  
  13.  
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. begin
  16.   edit1.clear;
  17.   edit2.clear;
  18.   assignfile(highscorefile, 'highhscore.dat');
  19.   if not fileexists('highhscore.dat') then
  20.   begin
  21.     rewrite(highscorefile);
  22.     closefile(highscorefile);
  23.   end;
  24.  
  25.  
  26.  
  27.   if fileexists('highscore.dat') then
  28.   begin
  29.    while not eof(highscorefile) do
  30.     begin
  31.     for i:=1 to 10 do
  32.     read(highscorefile, rank[i]);
  33.     end;
  34.   end;
  35.   for i:=1 to 10 do
  36.   if rank[i].score <> 0 then
  37.    begin
  38.     sgdata.Cells[1,i]:=rank[i].name;
  39.     sgdata.cells[2,i]:=inttostr(rank[i].score);
  40.    end;
  41.   end;                            

like this?

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: need help with calender
« Reply #82 on: September 23, 2017, 01:04:28 pm »
like this?
No. The problem in FormClose is that you can't use the while EOF when you are writing a file. while not EOF is only for reading and checking if the reading is at the end.

In FormClose you can just do a for-loop and write the 10 values to the file. B.T.W. You only want the top 10 right? You don't need a history of all other values, do you?

In FormCreate you have another problem. You first check if the file exists. If not you create it. After that you check if the file exists. It always does because you just created it (empty).

And in the while loop for reading the rank-values, you should do another 10 reads in that for loop. You need to set the initial value of I outside the while loop and increase it inside the while loop and read one value in rank[ i ].

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #83 on: September 23, 2017, 01:17:30 pm »
Code: Pascal  [Select][+][-]
  1. In FormCreate you have another problem. You first check if the file exists. If not you create it. After that you check if the file exists. It always does because you just created it (empty).
  2.  
  3. And in the while loop for reading the rank-values, you should do another 10 reads in that for loop. You need to set the initial value of I outside the while loop and increase it inside the while loop and read one value in rank[ i ].

so how should i code for those ones?

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: need help with calender
« Reply #84 on: September 23, 2017, 01:55:28 pm »
so how should i code for those ones?
You almost have the correct code.
The only thing is that you switched the if not fileexists and the if fileexists.
And that you read the rank array (10 records) for each while loop.

Try to step through the code and see what it does and what you want.

You only need
openfile
i = 1
while not eof
begin
  read 1 value
  if we have less than 10 rank values already then set the read value in rank[ i ]
  increase i by 1
end

Try to break down what you want in "pseudo code" like above and it will help you understand how to code it properly.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #85 on: September 23, 2017, 02:14:33 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   edit1.clear;
  4.   edit2.clear;
  5.   assignfile(highscorefile, 'highhscore.dat');
  6.   if not fileexists('highhscore.dat') then
  7.   begin
  8.     rewrite(highscorefile);
  9.     closefile(highscorefile);
  10.   end
  11.   else
  12.   begin
  13.   i:=1;
  14.    while not eof(highscorefile) do
  15.     begin
  16.     read(highscorefile, rank[1]);
  17.     if  i < 10 then read(highscorefile, rank[i]);
  18.     i:=i+1;
  19.     end;
  20.   end;
  21.  
  22.  
  23.   for i:=1 to 10 do
  24.   if rank[i].score <> 0 then
  25.    begin
  26.     sgdata.Cells[1,i]:=rank[i].name;
  27.     sgdata.cells[2,i]:=inttostr(rank[i].score);
  28.    end;
  29.   end;    

hi i cannot compile this

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: need help with calender
« Reply #86 on: September 23, 2017, 02:15:41 pm »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #87 on: September 23, 2017, 02:25:08 pm »
@rvk: sorry, not following the whole thread, but am i missing a reset ?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #88 on: September 23, 2017, 02:26:38 pm »
runerro 103 at the line i:i+1;

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: need help with calender
« Reply #89 on: September 23, 2017, 02:39:40 pm »
runerro 103 at the line i:i+1;
Line I := I + 1 can't give you a runtime 103 error because it doesn't do anything with the file.

But look at these lines:
Code: Pascal  [Select][+][-]
  1. read(highscorefile, rank[1]);
  2. if  i < 10 then read(highscorefile, rank[i]);
What is the first line for? You read in rank[1] ???

I said...
read in a value.
THEN check if you need to put that value in a rank position.

You have 2 read() actions (which results in an End Of File, EOF, in between them and there is no check for that)

 

TinyPortal © 2005-2018