Thread: Highscore Table

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Highscore Table

    Hello folks!

    I'm creating a wonderful word game and I want to create a highscore table too. I'm writing a highscore table with commands fread and fwrite but I'm getting a lot of errors. Please, give me some ideas how to write it.

    Thx in advance.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    4
    how about reading all the stuff into an array, then use a bubble sort(or as such) to sort it out ?
    how much work have you done on it ?

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I got this so far:

    Code:
    struct Table
    {
    	char Player[80];
    	float Points;
    };
    
    void Top10(int Category, float Points, char Player[])
    {
    	FILE *fin;
    	char file1[100], *file1ptr, TempPlayer[80];
    	struct Table s[10];
    	int i=0;
     	float TempPoints, Temp;
    
    	file1ptr = file1;
    	if (Category == 1)
    	{
    		file1ptr = "./DATA/POINTS/hil.dat";
    	}
    	else if (Category == 2)
    	{
    		file1ptr = "./DATA/POINTS/his.dat";
    	}
    	else if (Category == 3)
    	{
    		file1ptr = "./DATA/POINTS/hit.dat";
    	}
     	TempPoints = Points;
    	strncpy(TempPlayer, Player, 79);
    	fin = fopen(file1ptr, "wb");
    	for (i=0; i<1; i++)	
    	{	
    		fread(&s[i], sizeof(struct Table), 1, fin);
    		if (TempPoints <= s[i].Points)
    		{
    			Temp = TempPoints;
    			s[i].Points = Temp;
    			TempPoints = s[i].Points;
    		}
    		fwrite(&s[i], sizeof(struct Table), 1, fin);
    	}
    	fclose(fin);
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    4
    i havent looked at the actual code,
    have you initilised all the variabled in the file ?
    (do you have a file), if not maybe you need to create one,

    think about it.
    (im too tired sorry)

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Just look at the code, please.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    Unregistered
    Guest
    do you know your for loop goes around once ?

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Yes. This is beacuse the function is in the testing phase. The main problem is that when function write's into the file there are no values saved.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Unregistered
    Guest
    nah sorry m8, i cant help u.
    i havent done that much with file accessing.
    i dont see any sorting, would you put them into an array,
    then sort them there ?

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    fin = fopen(file1ptr, "wb");

    "w" create text file for writing; discard previous contents if any

    I suggest something like the following...
    Code:
    //Do the choice stuff up here....
    // ...
    
    // Read the file into an array.
    fin = fopen (file1pte, "rb");
    fread(&s[0], sizeof(struct Table), 10, fin);
    fclose (fin);
    
    for (i = 0; i < 10; i++)
    {
     // Place the new high score into the array.
    }
    
    // Write the array into the file.
    fin = fopen (file1ptr, "wb");
    fwrite (&s[0], sizeof (struct Table), 10, fin);
    
    return;
    }
    fclose (fin);
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I've written so far:

    Code:
    struct Table
    {
     char Player[80];
     float Points;
    };
    
    void Top10(int Category, float Points, char Player[])
    {
     FILE *fin;
     char file1[100], *file1ptr;
     struct Table s[10];
     int i=0;
     float TempPoints, Temp;
    
     file1ptr = file1;
    if (Category == 1)
    {
     file1ptr = "./DATA/TOCKE/hil.dat";
    }
    else if (Category == 2)
    {
     file1ptr = "./DATA/TOCKE/his.dat";
    }
    else if (Category == 3)
    {
     file1ptr = "./DATA/TOCKE/hit.dat";
    }
     TempPoints = Points;
    
     // reseting highscore table
     fin  = fopen(file1ptr, "wb");
     fread(&s[0], sizeof(struct Table), 10, fin);
     for (i=0; i<10; i++)
     {
     s[i].Points = 0.0;
     strncpy(s[i].Player, 0, 79); // if I have this here, than the error is   generated. WHY?
     }
     fwrite (&s[0], sizeof (struct Table), 10, fin);
     fclose(fin); 
     // sorting highscore table
     fin = fopen (file1ptr, "rb");
     fread(&s[0], sizeof(struct Table), 10, fin);
     for (i = 0; i < 10; i++)
     {
      if (TempPoints > s[i].Points)
     {					
     Temp = TempPoints;
     TempPoints = s[i].Points;
     s[i].Points = Temp;
     strncpy(s[i].Player, Player, 79);
     }
     }
     fin = fopen (file1ptr, "wb");
     fwrite (&s[0], sizeof (struct Table), 10, fin);
     fclose(fin);
     // print out of highscore table
     printf("\n");
     for (i=0; i<10; i++)
     {
     printf("\n%02d. %1.2f\t\t%s", i+1, s[i].Points, s[i].Player); 
     }
    This is working, but I have one error . This error is generated in the reseting section. Please take a look at the code .
    Last edited by GaPe; 01-30-2002 at 10:52 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #11
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I just discovered this. If I write this "strncpy(s[i].Player, "no name", 79);" instead of this "strncpy(s[i].Player, 0, 79);" then the code is working properly. Why? What is wrong with the second sentence?

    And another problem is bugging me.

    I want that this highscore table will print out the name of the player too, but with this code I do not get the right results.

    Code:
    // sorting highscore table
    fin = fopen (file1ptr, "rb");
    fread(&s[0], sizeof(struct Table), 10, fin);
    for (i = 0; i < 10; i++)
    {
      if (TempPoints > s[i].Points)
      {								  
    	Temp = TempPoints;
    	TempPoints = s[i].Points;		  
    	s[i].Points = Temp;
    	if (s[i].Points == Points) strncpy(si].Player, Player, 79);
      }
      else strncpy(s[i].Player, s[i].Player, 79);
    }
    fin = fopen (file1ptr, "wb");
    fwrite (&s[0], sizeof (struct Table), 10, fin);
    fclose(fin);
    // print out of the highscore table
    printf("\n");							
    for (i=0; i<10; i++)
    {
      printf("\n%02d. %-20s\t%1.2f", i+1, s[i].Player, s[i].Points); 
    }
    Please help.
    Last edited by GaPe; 01-30-2002 at 12:53 PM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Folks! I really don't know what is wrong with my code and I need your help. Thx in advance.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Ok, I figured it out. The problem was that I didn't save the previous name of the player into other variable.

    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  2. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM