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