Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TOP
#4
There are a few things. The following code for me prints both names with their corresponding values. Pretty much all I did was moving the print-lines below the foor-loop. Having it in the for-loop makes it print out one name twice and the other just once (Because in the first iteration, one name is obviously: "").

Code:
local top_winrate_1_name = "";
local top_winrate_1 = 0;
local top_winrate_2_name = "";
local top_winrate_2 = 0;

function ReadTOPRating()
{
    local reg_pl = 0;
   local file = fileOpen("RegisteredPlayers.txt","r");
   if (file)
   {
       local registered_players = fileRead(file,"d");
       reg_pl = registered_players.tointeger();
       fileClose(file);
   }

   file = fileOpen("Winrate.txt","r");
   for (local i = 0; i < reg_pl; ++i)
   {
       local name = fileRead(file);
       local rating = fileRead(file).tointeger();
       if (rating > top_winrate_1)
       {
           top_winrate_1_name = name;
           top_winrate_1 = rating;
       }
       else if (rating > top_winrate_2)
       {
           top_winrate_2_name = name;
           top_winrate_2 = rating;
       }
   }
    
    print(top_winrate_1_name);
    print(top_winrate_1);
    print("------------");
    print(top_winrate_2_name);
    print(top_winrate_2);
}


However, there are better ways to read your values from your file. You don't have to save the number of registered players. I would rather do something like this:

Code:
local read = fileRead(file);
while(read)
{
   DoSomethingWithTheValues();

    // Right at the end of the loop. read will be null if no new line could be found, which then ends the loop
   read = fileRead(file);
}
This way you can be sure that your whole file will be read and you just use one file.
Reply


Messages In This Thread
TOP - by Osmith - 01.05.2016, 20:03
RE: TOP - by HammelGammel - 01.05.2016, 22:12
RE: TOP - by Osmith - 01.05.2016, 23:09
RE: TOP - by HammelGammel - 02.05.2016, 02:29
RE: TOP - by Osmith - 02.05.2016, 10:14
RE: TOP - by HammelGammel - 02.05.2016, 12:16

Forum Jump:


Users browsing this thread: 1 Guest(s)