| | |
Help With Making A High Scores Table
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
i know the code is probably disastrous but i was hoping help could be given with what sense you can make out of it
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
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.
Last edited by Lerner; Dec 9th, 2008 at 12:55 pm.
Klatu Barada Nikto
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
Klatu Barada Nikto
![]() |
Other Threads in the C++ Forum
- Previous Thread: Reading from text file into array
- Next Thread: strings as input, substr and len
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






