Thread: HighScores Menu

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    6

    HighScores Menu

    Hi everybody im doing this game project and i got stock in the last part.
    so the problem is that is i can make scores to sort from highest to lowest but the name of players will eventually printf in the order it was scanned how can relate this 2 to every player's score print with their own name from highest to lowest
    here is my code

    Code:
        Scores[Counter2]=HIGHSCORE;    
    
    TheLastOne[Counter2]=HIGHSCORE; 
           char Name[20][20];/*names are scanned and store in this array in another part of code*/
    
        for (j=0 ; j<19 ; j++)
        {
            for (i=0 ; i<19 ; i++)
            {
                if (Scores[i+1] < Scores[i])
                {
                    t = Scores[i];
                    Scores[i] = Scores[i + 1];
                    Scores[i + 1] = t;
    
    
                }
            }
        }
        system("cls");
        for (i=20 ; i>=20-Counter2 ; i--)
        {
            printf ("%d-%s=>%d\n",21-i,Name[20-i],Scores[i-1]);
        }
    i tried store the names in another array by comparing them with this code below but didnt work
    Code:
            for(int k=0;k<20;k++)
        {
                for(int z=0;z<20;z++)
                {
                    if(TheLastOne[k]==Scores[z])
                        for(int u=0;u<20;u++)
                        {
                            Name2[u][k]=Name[u][19-z];
                        }
                
                }
        }
    Last edited by parham-box; 12-02-2016 at 03:04 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to relate the names with scores. Having them in two separate arrays is, as you can see, difficult to keep track of.

    This is where a struct would come in handy, for example:
    Code:
    struct Player
    {
        char name[20];
        int score;
    };
    Then have an array of those and sort them appropriately:
    Code:
    Player gamePlayers[MAX_PLAYERS];
    You could just as easily define a function "comparePlayer()" in order to pass it to "qsort()" for fast and easy sorting.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    6
    thanks so much for your respond but the problem is they dont allow us in college to use struct cause they havent teach it to us yet

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Then how about this:
    Each time you change the index position of a score, while you try to sort it of course, make the same change for the name. I see you have implemented bubble-sort, so inside that if statement you could also swap the contents of Name[i] and Name[i+1], by using "strcpy()" and a temporary array.
    Devoted my life to programming...

  5. #5
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Use a std::pair<int,std::string> and use your bubblesort to sort by the int part of the pair.
    std::pair

  6. #6
    Registered User
    Join Date
    Nov 2016
    Posts
    6
    so i tried to do this
    Code:
        for(int k=0;k<20;k++)
        {
            for(int z=0;z<20;z++)
            {
                if(TheLastOne[k]==Scores[z])
                {
                    strcpy(Name2[k],Name[k]);
                    strcpy(Name[k],Name[19-z]);
                    strcpy(Name[19-z],Name2[k]);
                }
             
            }
        }
            printf ("%d-%s=>%d\n",21-i,Name[20-i],Scores[i-1]);
    but didnt work still printing in the order its get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-16-2015, 02:32 PM
  2. help menu
    By a.mlw.walker in forum C Programming
    Replies: 1
    Last Post: 03-27-2009, 06:06 AM
  3. Menu?
    By killmr in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2005, 08:45 AM
  4. Menu
    By bahumat_12 in forum C Programming
    Replies: 4
    Last Post: 02-22-2004, 03:16 AM
  5. How to modify a menu into a menu Folder(contains submenus) ??
    By L.O.K. in forum Windows Programming
    Replies: 3
    Last Post: 01-09-2003, 02:26 PM

Tags for this Thread