So far I have a working menu, which lets you enter in your name, highscore and date which then gets saved to a file and is outputted via another option on the menu. I had a lot of trouble gettin the user info to save to a file in the first place, then getting multiple user info to save in the same file etc, but now that I have got that I just need help sorting the high score entries into order of score, but I have no idea how to go about it! I'll post my code below, and if you look at it you will see that the top line of each high score entry looks something like "#------"#"-----#. The middle "#" is what I want to make the number of the entry, for example if that entry is the top score, it should show "#------"1"------#" etc.
I'm sorry if I haven't explained very well, if anything is unclear please just ask! Here is my code:
Expand|Select|Wrap|Line Numbers
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<cstdlib>
- #include"date_type.h"
- using namespace std;
- struct player {
- string name;
- int score, dD, dM, dY;
- };
- player playerList[20];
- int playerNumber=0;
- void addPlayer(player user[], int size, int const track);
- void showScores(player user[], int size, int const track);
- string getString(string prompt) {
- string s;
- cout << prompt;
- getline(cin, s);
- return s;
- }
- int menu(int &menuChoice)
- {
- do {
- cout << endl << "#--- HIGH-SCORE MENU ---#" << endl;
- cout << endl << "1) Add a High-Score entry";
- cout << endl << "2) Display the High Score table";
- cout << endl << "3) Delete current High Scores";
- cout << endl << "4) Quit the program" << endl;
- cout << endl << "Please enter your choice (1-5): ";
- cin >> menuChoice;
- cout << endl;
- if( menuChoice > 4 || menuChoice < 1) {
- cout << "Please select an existing option!" << endl;
- }
- } while( menuChoice > 4 || menuChoice < 1);
- return menuChoice;
- }
- int main()
- {
- int menuChoice;
- int static track = 0;
- player user[50];
- do {
- menu(menuChoice);
- switch(menuChoice)
- {
- case 1:
- addPlayer(user, 20, track);
- track++;
- break;
- case 2:
- showScores(user, 20, track);
- break;
- case 3:
- ofstream SaveFile("playerInfo.txt", ios::trunc);
- SaveFile.close();
- cout << "High Scores successfully deleted" << endl;
- break;
- }
- } while(menuChoice != 4);
- }
- void addPlayer(player user[], int size, int const track)
- {
- player p;
- cout << "Enter your nickname: ";
- cin >> user[track].name;
- cout << "Enter your score: ";
- cin >> user[track].score;
- do {
- cout << "Enter date (dd mm yy): ";
- cin >> user[track].dD >> user[track].dM >> user[track].dY;
- if(user[track].dD < 1) {
- cout << "Please select a correct date (day is invalid)!" << endl;
- }
- if(user[track].dM > 12 || user[track].dM < 1) {
- cout << "Please select a correct date (month is invalid)!" << endl;
- }
- if(user[track].dY > 2100 || user[track].dY < 1900) {
- cout << "Please select a correct date (year is unrealistic)!" << endl;
- }
- if(user[track].dM == 1) { if(user[track].dD > 31) { cout << "Only 31 days in January!" << endl;} }
- if(user[track].dM == 2) { if(user[track].dD > 29) { cout << "Only 29 days in February!" << endl;} }
- if(user[track].dM == 3) { if(user[track].dD > 31) { cout << "Only 31 days in March!" << endl;} }
- if(user[track].dM == 4) { if(user[track].dD > 30) { cout << "Only 30 days in April!" << endl;} }
- if(user[track].dM == 5) { if(user[track].dD > 31) { cout << "Only 31 days in May!" << endl;} }
- if(user[track].dM == 6) { if(user[track].dD > 30) { cout << "Only 30 days in June!" << endl;} }
- if(user[track].dM == 7) { if(user[track].dD > 31) { cout << "Only 31 days in July!" << endl;} }
- if(user[track].dM == 8) { if(user[track].dD > 31) { cout << "Only 31 days in August!" << endl;} }
- if(user[track].dM == 9) { if(user[track].dD > 30) { cout << "Only 30 days in September!" << endl;} }
- if(user[track].dM == 10) { if(user[track].dD > 31) { cout << "Only 31 days in October!" << endl;} }
- if(user[track].dM == 11) { if(user[track].dD > 30) { cout << "Only 30 days in November!" << endl;} }
- if(user[track].dM == 12) { if(user[track].dD > 31) { cout << "Only 31 days in December!" << endl;} }
- } while(user[track].dM == 1, 3, 5, 7, 8, 10, 12 && user[track].dD > 31 ||
- user[track].dM == 2 && user[track].dD > 29 ||
- user[track].dM == 4, 6, 9, 11 && user[track].dD > 30 ||
- user[track].dD < 1 ||
- user[track].dM > 12 ||
- user[track].dM < 1 ||
- user[track].dY > 2100 ||
- user[track].dY < 1900);
- cout << endl << "Thanks for your entry!" << endl;
- ofstream playerInfo("playerInfo.txt", ios::app);
- playerInfo << "#---------------'" << "#" << "'--------------#" << endl
- << "#--------Nickname: " << user[track].name << endl
- << "#--------Score Achieved: " << user[track].score << endl
- << "#--------Date Achieved: " << user[track].dD << "/" << user[track].dM << "/" << user[track].dY << endl
- << "#--------------------------------#" << endl;
- playerInfo.close();
- }
- void showScores(player user[], int size, int const track)
- {
- int count;
- string line;
- cout << endl
- << "#--------------------------------#" << endl
- << "#-----******HIGHSCORES******-----#" << endl
- << "#--------------------------------#" << endl;
- ifstream playerInfo("playerInfo.txt");
- if (playerInfo.is_open())
- {
- while (! playerInfo.eof() )
- {
- getline (playerInfo,line);
- cout << line << endl;
- }
- }
- else cout << "Unable to open file";
- playerInfo.close();
- return;
- }