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;
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 {
//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: