Hi, i am in the process of trying to make a high scores table but i am failing miserably, i dont have a lot of experience in c++ and i was hoping i could get a bit of help trying to sort my code.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct player {    
	string nickname;
	int score;
	string date;
};
player playList[20]; 
int numPlayers=20;


player getPlayer() // input of data
{
	player p;
	cout << "Enter players nickname:";
	cin >> p.nickname;
	cout << "Enter players score:";
	cin >> p.score;
	cout << "Enter date this score achieved:";
	cin >> p.date;
	cin.ignore ();
	return p;
}

void showPlayer(player p) 
{
    cout << "Nickname:" << p.nickname << endl << "Score:" << p.score << endl << "Date:" << p.date <<  endl;

void sortplayList(playList[], int score);
	
	int pos;
	player score;
	bool swap;
	int size;
	do{
		swap=false;
		for(pos=0; pos<(size-1); pos++)
		{
			if(playList[pos].score>playList[pos+1].score)
			{
				score = playList[pos];
				playList[pos] = playList[pos+1];
				playList[pos+1] = score;
				swap=true;
			}
		}
	} while(swap);
}

void sendtofile(ofstream &file, player p)
{
	file << p.nickname << endl 
		 << p.score << endl
		 << p.date << endl;
}

player getfromfile(ifstream &file)
{
	player p;
	file >> p.nickname;
    file >> p.score;
	file >> p.date;
	file.ignore();
	return p; 
}

void saveList()
{
	ofstream ofs("h:\\Into to Programming\\hiscore.txt", ios::out);
	ofs << numPlayers << endl;
	for(int i=0; i<numPlayers; i++)
		sendtofile(ofs, playList[i]);
	ofs.close();
}

void loadList()
{
	try{
		ifstream ifs("h:\\Intro to Programming\\hiscore.txt", ios::in);
		ifs >> numPlayers;
		ifs.ignore(); 
		for(int i=0; i<numPlayers; i++)
			playList[i] = getfromfile(ifs);
		ifs.close();
	}
	catch(...){
		cout << "No data file.";
	}
}

void printList()
{
	for(int i=0; i<numPlayers; i++)
		showPlayer(playList[i]);
}

void menu()
{
	char choice;
	do{
	cout << "1) Load High Score Table." << endl;
	cout << "2) Save High Score Table." << endl;
	cout << "3) Add A High Score Entry." << endl;
	cout << "4) Display The High Score Table." << endl;
	cout << "5) Quit Program." << endl;
	cout << "Choose an option.";
	cin >> choice;
	cin.ignore();
	switch(choice)
	{
	case '1':
		loadList();
		break;
	case '2':
		saveList();
		break;
	case '3':
		getPlayer();
		numPlayers++;
		break;
	case '4':
        printList();
		break;	
 default:
		;
	}
	} while(choice!='5');
}

void main()
{
	loadList();
    menu();
	saveList();
		
}

i know the code is probably disastrous but i was hoping help could be given with what sense you can make out of it

Frist, it's int main(), not void main(), for portability purposes, meaning, even if your compiler lets you use void, don't.

Second, you need a closing curly brace between the end of showplalyer() and the first line of sortplayList().

Third, you need to indicate what type playlist[] is in the first line of sortplayList()

Fourth, in sortplayList() you have two variables with the same name----- int score and player score. How is the compiler going to konw which one to use. Since you don't need the int score one, why bother to send it to the function?

Fifth, you don't intialize the variable called size befor you try to use it. I suspect you want to pass size to the function instead of score, but only you will know for sure when you evaluate things

Sixth, don't put a semicolon after the first line of sortplayList

Seventh, do put an opening curly brace after the first line of sortplayList()l

Eighth, who knows what will pop up when you fix all of those.

thanks a lot that has helped quite a bit.
i still have a few errors mainly in the void sortplayList function which i think is the part causing the problems mostly.
is there any other problems you can see particularly in that section?

Well, it's not the way I usually write a bubble sort for an array, but it looks like it might work. Print out the array before and after you sort it to see for yourself if it works or not. That's another step to debugging your own code and should be part of your program development process. Plan, write one function (or less) at a time, debug syntax, debug runtime, repeat until output is at desired level for a given set of input and then follow that up by running with additional inputs to do boundary testing represents a reasonable start for the developmental cycle of a relatively simple program. If you skimp at any level you are asking for problems.

thanks, that has helped me to at least get the program running, now just to get it validated and the save/load feature working and it should be fine, your help is much appreciated thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.