Sorting Scores from highest to lowest in hangman game

I want to sorting scores while the player playing hangman game.. while the player stop the game then in file score.txt will show the score of the player from the highest score to lowest score. I'm stuck and Need help please..

int main() {
int tingkat, game1;
string player1;
char yt,keluar;
cout << "===================================================================================" <<endl;
cout << "||====================WELCOME TO PROGRAM ROG GAME HANGMAN!================||" <<endl;
cout << "===================================================================================" <<endl;
cout<<"============================================="<<endl;
cout<<"|| HANGMAN GAME ||"<<endl;
cout<<"============================================="<<endl;
cout<<"|| 1) Play Game ||"<<endl;
cout<<"|| *Bermain Harus Mengunakan Huruf Kapital ||"<<endl;
cout<<"|| 2) Exit Game ||"<<endl;
cout<<"============================================="<<endl;

cout<<"Input your choices : ";
cin>>game1;

switch(game1)
{
case 1: cout<<"Start Game?[Y/T] :"; cin>>yt;
cout<<endl;
if(yt=='Y'||yt=='y')
{goto bawah;}

else if ((yt == 'T') || (yt == 't'))
{
cout << "\n\tOk, See you later! ";
return 0;
}
bawah:
cout<<"Name of Player :"; cin>>player1;




break;
case 2 : cout<<"Are you sure to exit game ?[Y/T] :"; cin>>keluar;
if ((keluar == 'Y') || (keluar == 'y'))
{
cout << "\n\tOk, Thank you have play this game! see you! ";
return 0;
}
else if((keluar =='T') || (keluar == 't'))
{
cout<<endl;
cout<<"Input the player's name :"; cin>>player1;
}

break;

}
cout<<endl;

cout<<"============================================"<<endl;
cout<<"| Level of Games : |"<<endl;
cout<<"| 1. Mudah(Easy) |"<<endl;
cout<<"| 2. Sedang(Medium) |"<<endl;
cout<<"| 3. Sulit(Hard) |"<<endl;
cout<<"============================================"<<endl;
cout<<endl;
cout<<"Choose the level you want :";
cin>>tingkat;

if(tingkat==1){
string tingkat;
tingkat = "easy";
ifstream myFile("level.txt");
if (myFile.fail()) {
cout<<" File not found!";
}
else {


std::ifstream in("easy.txt");

std::vector<string> kata(( std::istream_iterator<string>(in)), std::istream_iterator<string>() );

string s;

int score = 0;
bool gameOver = 0;
int level;
for (int i = 0, level = 1; (unsigned) i < kata.size() && gameOver == 0; i += 2) {
cout << "==============="<<endl;
cout << "|| Level : "<< level++ << " ||"<<endl;
cout << "===============" << endl; // display the question number
cout << "|| SCORE: " << score <<" ||" << endl;
cout << "===============" << endl;


bool pertandingan = 0; // stop when word is complete

string ruang = buatRuang(kata[i].length()); // make a space to fill

int salah = 0; // count wrongs

cout << kata[i+1] << endl << endl; // the clue

cout << ruang << endl; // the space, stars

while (salah < 5 && pertandingan == 0) {
cin >> s;

s = hapus(s);

for (int j = 0; (unsigned) j < s.length(); j++) {
if (tampilan(s[j], kata[i]) == 1) {
ruang = karakter(s[j], ruang, kata[i]);
} else {
salah++;
}
}

cout << ruang << endl;

if (ruang == kata[i]) {
pertandingan = 1;
score += 10-2*salah;
cout << endl << endl;
} else if (salah >= 5) {
gameOver = 1;
cout << endl << endl;
}
}
}

cout << "GAME OVER!"<<endl
<< "Oh No! You Loose! Try Again!!!" << endl
<< "==========================================="<< endl
<< "SCORE: " << score << endl
<< endl << endl;

//stream to write file
ofstream myfile;

//open file,
//while file not found then will be made automatic
myfile.open("scor.txt", ios::app);

//fail() -> check error while operating file
if(!myfile.fail())
{
//writing to file
myfile<<player1<<"\t"<<score<<"\t"<<level<<endl;
myfile.close(); //close file
//cout<<"Text has been written to the file"<<endl;
}else{
cout<<"File not found"<<endl;
}

}


}
Make a function that does this and pick a spot in the code where you want it to happen (where you'll call the function from).

Assuming you're already outputting the scores onto the file, the function will simply take in the input (scores) and sort them from highest to lowest, preferably with an array/vector. Here's an example:

scores.txt:
10
60
40
100
25
70
55


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	std::ifstream inFile;
	inFile.open("scores.txt");
	
	int scores[7];

	for (int i = 0; i < 7; i++)
		inFile >> scores[i];

	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 7; j++)
		{
			if (scores[i] > scores[j])
			{
				std::swap(scores[i], scores[j]);
			}
		}
	}

	for (int i = 0; i < 7; i++)
		std::cout << scores[i] << ' ';
}


Output:

100 70 60 55 40 25 10
Topic archived. No new replies allowed.