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: - #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;
-
}
3 8900
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.
donbock 2,426
Recognized Expert Top Contributor
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 percentile) of an item,
given something called a z-score. If I have a z-score, I can look up the
probability in a distribution table.
There are plenty of algorithms around to do that calculation, but I need to
do a reverse lookup. I am given a...
|
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 that i could
create an object to compare to these rules in the database.
|
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 scores get corrupted and so when i again takes the turbo c++ , the files which i have written to the file are all missing . please help me . here is the code
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
struct...
|
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 game instead
of a high score for a particular user, session, or computer. Perhaps
someone already has code written to do this. Any help would be
greatly appreciated.
|
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 display the score at the
end of the game. At the moment when a player finishes the game I have a
window displaying "Puzzle Solved." If I could display the players score
under this in the same window would be great.
This is a quick copy and paste...
| |
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 read from the file?
I have tried the
stringname.find("Name");
This only returns the first occurence, and doesn't look for other
occurences.
|
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 how I could get it to work with the data in my vector.
Here is a sample high score file in which each line is inputted into a vector element. I want to sort by the score:
2345 toefraz day time year
1009 toe day time year...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |