I am currently developing a simple application to count the number of clicks within 10 seconds, read in names and scores from two different files, then display those names and scores with various sorts in another form. However, after the game is finished, the 'Game Over!' message appears, the Leaderboard form appears but then has no buttons, looks oddly flat and appears to crash the program.
This is how the form should look: Leaderboard Form
And this is how it actually looks: Leaderboard Error in Form
Code for displaying the form can be found below:
if TimeLeft=0 then
begin
Form2.Timer1.Enabled:=False;{Disable timer}
ShowMessage('Game Over!');{Message to show upon termination condtion being met}
Leaderboard.Show;{Show Leaderboard Form}
Form2.Hide;{Hide game}
Reset(LeaderboardNamesFile);{Open file}
while not EOF(LeaderboardNamesFile) do
LineCount:=LineCount+1;{Increment to allow for EOF marking of the score}
LeaderboardScoresArray[LineCount]:=Score;{Add score to array of scores}
end;
And the code contained in the Score display button on the Leaderboard form:
var Counter : Integer;
begin
Counter:=1;
Memo1.Lines.add(LeaderboardNamesArray[Counter]+' - '+IntToStr(LeaderboardScoresArray[Counter]));
end;
I find this all very strange, as nothing actually runs when the form is shown, so it should crash before this if ever, and no crash messages appear. Any ideas? If more info if needed, please ask. New on this site!
The while loop is endless, the loop contains only one command:
while not EOF(LeaderboardNamesFile) do
LineCount:=LineCount+1;{Increment to allow for EOF marking of the score}
So, your program count LineCount up but don't aktually read any data from the file. So the file never becomes EOF ("end of file"). You need to do something like this:
Reset(LeaderboardNamesFile);{Open file}
while not EOF(LeaderboardNamesFile) do
begin
LineCount:=LineCount+1;{Increment to allow for EOF marking of the score}
readln(LeaderboardNamesFile, Score);
LeaderboardScoresArray[LineCount]:=Score;{Add score to array of scores}
end;
CloseFile(LeaderboardNamesFile);