473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

High Score table help

Ok, so for a project at university I have to create a High Score table in C++ with Visual Studio. Just to let you know this is my first year at university and also my first time ever learning C++.
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
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<cstdlib>
  5. #include"date_type.h"
  6. using namespace std;
  7.  
  8. struct player {
  9.     string name;
  10.     int score, dD, dM, dY;
  11. };
  12.  
  13. player playerList[20];
  14. int playerNumber=0;
  15.  
  16. void addPlayer(player user[], int size, int const track);
  17. void showScores(player user[], int size, int const track);
  18.  
  19. string getString(string prompt) {
  20.     string s;
  21.     cout << prompt;
  22.     getline(cin, s);
  23.     return s;
  24. }
  25.  
  26. int menu(int &menuChoice)
  27. {
  28.     do {
  29.         cout << endl << "#--- HIGH-SCORE MENU ---#" << endl;
  30.         cout << endl << "1) Add a High-Score entry";
  31.         cout << endl << "2) Display the High Score table";
  32.         cout << endl << "3) Delete current High Scores";
  33.         cout << endl << "4) Quit the program" << endl;
  34.         cout << endl << "Please enter your choice (1-5): ";
  35.         cin >> menuChoice;
  36.         cout << endl;
  37.         if( menuChoice > 4 || menuChoice < 1) {
  38.             cout << "Please select an existing option!" << endl;
  39.         }
  40.     } while( menuChoice > 4 || menuChoice < 1);
  41.     return menuChoice;
  42. }
  43.  
  44. int main()
  45. {
  46.     int menuChoice;
  47.     int static track = 0;
  48.     player user[50];
  49.     do {
  50.         menu(menuChoice);
  51.         switch(menuChoice)
  52.         {
  53.         case 1:
  54.             addPlayer(user, 20, track);
  55.             track++;
  56.             break;
  57.         case 2:
  58.             showScores(user, 20, track);
  59.             break;
  60.         case 3:
  61.             ofstream SaveFile("playerInfo.txt", ios::trunc);
  62.             SaveFile.close();
  63.             cout << "High Scores successfully deleted" << endl;
  64.             break;
  65.         }
  66.     } while(menuChoice != 4);
  67. }
  68.  
  69. void addPlayer(player user[], int size, int const track)
  70. {
  71.     player p;
  72.     cout << "Enter your nickname: ";
  73.     cin >> user[track].name;
  74.     cout << "Enter your score: ";
  75.     cin >> user[track].score;
  76.  
  77.     do {
  78.         cout << "Enter date (dd mm yy): ";
  79.         cin >> user[track].dD >> user[track].dM >> user[track].dY;
  80.         if(user[track].dD < 1) {
  81.                 cout << "Please select a correct date (day is invalid)!" << endl;
  82.         }
  83.         if(user[track].dM > 12 || user[track].dM < 1) {
  84.                 cout << "Please select a correct date (month is invalid)!" << endl;
  85.         }
  86.         if(user[track].dY > 2100 || user[track].dY < 1900) {
  87.                 cout << "Please select a correct date (year is unrealistic)!" << endl;
  88.         }
  89.         if(user[track].dM == 1) { if(user[track].dD > 31) { cout << "Only 31 days in January!" << endl;} }
  90.         if(user[track].dM == 2) { if(user[track].dD > 29) { cout << "Only 29 days in February!" << endl;} }
  91.         if(user[track].dM == 3) { if(user[track].dD > 31) { cout << "Only 31 days in March!" << endl;} }
  92.         if(user[track].dM == 4) { if(user[track].dD > 30) { cout << "Only 30 days in April!" << endl;} }
  93.         if(user[track].dM == 5) { if(user[track].dD > 31) { cout << "Only 31 days in May!" << endl;} }
  94.         if(user[track].dM == 6) { if(user[track].dD > 30) { cout << "Only 30 days in June!" << endl;} }
  95.         if(user[track].dM == 7) { if(user[track].dD > 31) { cout << "Only 31 days in July!" << endl;} }
  96.         if(user[track].dM == 8) { if(user[track].dD > 31) { cout << "Only 31 days in August!" << endl;} }
  97.         if(user[track].dM == 9) { if(user[track].dD > 30) { cout << "Only 30 days in September!" << endl;} }
  98.         if(user[track].dM == 10) { if(user[track].dD > 31) { cout << "Only 31 days in October!" << endl;} }
  99.         if(user[track].dM == 11) { if(user[track].dD > 30) { cout << "Only 30 days in November!" << endl;} }
  100.         if(user[track].dM == 12) { if(user[track].dD > 31) { cout << "Only 31 days in December!" << endl;} }
  101.     } while(user[track].dM == 1, 3, 5, 7, 8, 10, 12 && user[track].dD > 31 ||
  102.             user[track].dM == 2 && user[track].dD > 29 ||
  103.             user[track].dM == 4, 6, 9, 11 && user[track].dD > 30 ||
  104.             user[track].dD < 1 ||
  105.             user[track].dM > 12 ||
  106.             user[track].dM < 1 ||
  107.             user[track].dY > 2100 ||
  108.             user[track].dY < 1900);
  109.     cout << endl << "Thanks for your entry!" << endl;
  110.     ofstream playerInfo("playerInfo.txt", ios::app);
  111.     playerInfo << "#---------------'" << "#" << "'--------------#" << endl
  112.                   << "#--------Nickname: " << user[track].name << endl
  113.                   << "#--------Score Achieved: " << user[track].score << endl
  114.                   << "#--------Date Achieved: " << user[track].dD << "/" << user[track].dM << "/" << user[track].dY << endl
  115.                   << "#--------------------------------#" << endl;
  116.     playerInfo.close();
  117. }
  118.  
  119. void showScores(player user[], int size, int const track)
  120. {
  121.     int count;
  122.     string line;
  123.     cout << endl
  124.          << "#--------------------------------#" << endl
  125.          << "#-----******HIGHSCORES******-----#" << endl
  126.          << "#--------------------------------#" << endl;
  127.                  ifstream playerInfo("playerInfo.txt");
  128.                 if (playerInfo.is_open())
  129.                 {
  130.                     while (! playerInfo.eof() )
  131.                     {
  132.                         getline (playerInfo,line);
  133.                         cout << line << endl;
  134.                     }
  135.                 }
  136.                 else cout << "Unable to open file"; 
  137.                 playerInfo.close();
  138.          return;
  139. }
Dec 10 '08 #1
3 8846
weaknessforcats
9,208 Expert Mod 8TB
The easiest sort algorithm is to traverse the array and compare adjacent elements (using a funcittion you write that compares two players) and if the first element is less than the second, swap the elements. Just repeat traversing the array until there are no swaps and you are sorted.

You might research various sort algorithms. Bubble Sort is a common choice and is similar to the above algorithm.
Dec 10 '08 #2
donbock
2,426 Expert 2GB
It sounds like you have a two-step operation: first, parse the input file into an array of scoring information; second, sort that array. Each entry in the array of scoring information is a structure that contains [at least] high score and username.

Parsing a text file is a very different task than sorting an array. Which one do you want to work on first?

Run several different input files through your parsing program and print the array contents. You're done when the printout matches the input.

To work on the sorting task you should hard-code initial values into the scoring array. Run several different sets of initial values through your sorting program and print the result. You're done when the printout shows the proper order.

In either case, you need to start by defining the scoring information structure.
Dec 10 '08 #3
donbock
2,426 Expert 2GB
Refer to the following Insight article for advice on how to parse a file:
How to parse a file in C++
Dec 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dave Veeneman | last post by:
I'm looking from an algorithm that will do a statistics calculation for me-- it's the first time 'Numerical Recipies in C' has failed me! A normal distribution table tells me the probability (by...
3
by: hazz | last post by:
if i have a table record called Rule 1 with 1. table name 2. table column name 3. operator (>,< =) 4. value (0 to n) 5. score how could I create code to create a rule automatically so...
0
by: asrock | last post by:
i have prepared a code in c++ to save high scores and it is working well when i compile it in turbo c++and run the code . but when i exit from the turbo c++ , the file to which i saved the high...
2
by: Anthony | last post by:
I have a team website that has several games for entertainment. One of the games is a javascript version of yahtzee. Can someone suggest a way, using javascript, to record a high score for the...
1
by: Flanders | last post by:
I have developed a small arcade game with the help of a few VB books. A scoring system was implemented in the design of the game but I as hoping that some one would be able to instruct me on how...
4
by: kyle christian | last post by:
I am trying to save the high scores of a game I made. Now Im stumped on trying to search through the file. Should I use a string, array, or one of the STL containers to manipulate the information...
8
by: toefraz | last post by:
Hi, guys. I'm writing a little game, and I want to include a high scores list. I've got everything coded except for the sorting part. I want to use the std::sort() function, but I'm not quite sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.