Im am trying to write a high score list . The user is suppose to enter name and score that will be sorted..

Emma 500
Andy 400
Linda 300

there will be room for five names on the list... I know how to make the program work to sort numbers but how can I "connect" the name to the number ? Like if the user first enters the name that is being placed in one array and then the score that is placed in another array and then sorting to make the scores in order...

This is my code for just sorting numbers...I am a beginner at c++ just so you know..

#include <iostream>

using namespace std;

int main()

	int myArray[5];
	cout << "score:\n";
	cin  >> myArray[4];
	cout << "\n";

      int i;
	  int j;
	  int flag = 1;   
      int temp;      // behållare för värde i arrayen
	  int a;

      int numLength =5; // längden på arrayen 

      for(i = 1; (i <= numLength) && flag; i++)// så länge i är mindre eller lika med 5 + flaggan, körs loopen. 
          flag = 0;

          for (j=0; j < (numLength -1); j++)        // loopen kör så länge j är mindre än 4 
			 if (myArray[j+1] > myArray[j])      // villkor som kontrollerar vilka värden som jämförs 
                    temp = myArray[j];             // byter plats på element i arrayen
                    myArray[j] = myArray[j+1];
                    myArray[j+1] = temp; 
                    flag = 1;               // visar att ett byte är gjort och att loppen ska köras igen/ om inget byte är gjort har flag värde 0 och arrayen är sorterad 



	  for (a=0; a<5;a++)
		  cout << myArray[a] << "\n";


return 0;

5 Years
Discussion Span
Last Post by emmaand

If you use two arrays, one for the scores and another for the names, during the sort you have to swap both arrays at the same time so that their order remains the same in both arrays. This is one reason why its much simpler to just use an array of structures, then swap the entire structure, all at the same time.


ok, but I dont understand the logic like how to swap the name like if the array looks like this :

int array[8]
array[0]=Andy;  array[1]=500;
array[2]=Sara;  array[3]=400;
array[4]=Linus; array[5]=100;
array[6]=Joey;  array[7]=300;

Then I compere array[5] to array[7]   
if 300 > 100 swap... and then I get :

array[0]=Andy;  array[1]=500;
array[2]=Sara;  array[3]=400;
array[4]=Linus; array[5]=300;
array[6]=Joey;  array[7]=100;

But now It will look like Linus had 300p , how to I get Joey to follow 300 if you understand what I mean?


You have to use two different arrays. You can't put names (text) in an array of integers

#include <string>

int main()
   int scores[5];
   std::string names[5];

   // swap the two arrays
   if( scores[j+1] > scores[j] )
      std::string tempname;
      int tempscore;
      tempscore = scores[j+1];
      scores[i] = scores[j];
      scores[j] = tempscore;
      // swap the names
      tempname = names[j+1];
      names[i] = names[j];
      names[j] = tempname;

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.