GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Dec-2008, 09:46
hamishmcgee hamishmcgee is offline
New Member
 
Join Date: Dec 2008
Posts: 1
hamishmcgee is on a distinguished road

Ordering saved file entries


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:

CPP / C++ / C 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;
}
  #2  
Old 16-Dec-2008, 23:04
genesaika genesaika is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 80
genesaika is on a distinguished road

Re: Ordering saved file entries


Well I can't say for sure but this seems like you would tell the program to open the file and then( with a simillar code to a basic number organizer) organize the entries.

I however don't know for sure if what I said is even right.

I hope this helps you
Gene Saika
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Double linked List & File System NatsoumiMaya C Programming Language 1 10-Feb-2008 09:23
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.