Help With Making A High Scores Table

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 3
Reputation: gingo is an unknown quantity at this point 
Solved Threads: 0
gingo gingo is offline Offline
Newbie Poster

Help With Making A High Scores Table

 
0
  #1
Dec 9th, 2008
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.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct player {
  7. string nickname;
  8. int score;
  9. string date;
  10. };
  11. player playList[20];
  12. int numPlayers=20;
  13.  
  14.  
  15. player getPlayer() // input of data
  16. {
  17. player p;
  18. cout << "Enter players nickname:";
  19. cin >> p.nickname;
  20. cout << "Enter players score:";
  21. cin >> p.score;
  22. cout << "Enter date this score achieved:";
  23. cin >> p.date;
  24. cin.ignore ();
  25. return p;
  26. }
  27.  
  28. void showPlayer(player p)
  29. {
  30. cout << "Nickname:" << p.nickname << endl << "Score:" << p.score << endl << "Date:" << p.date << endl;
  31.  
  32. void sortplayList(playList[], int score);
  33.  
  34. int pos;
  35. player score;
  36. bool swap;
  37. int size;
  38. do{
  39. swap=false;
  40. for(pos=0; pos<(size-1); pos++)
  41. {
  42. if(playList[pos].score>playList[pos+1].score)
  43. {
  44. score = playList[pos];
  45. playList[pos] = playList[pos+1];
  46. playList[pos+1] = score;
  47. swap=true;
  48. }
  49. }
  50. } while(swap);
  51. }
  52.  
  53. void sendtofile(ofstream &file, player p)
  54. {
  55. file << p.nickname << endl
  56. << p.score << endl
  57. << p.date << endl;
  58. }
  59.  
  60. player getfromfile(ifstream &file)
  61. {
  62. player p;
  63. file >> p.nickname;
  64. file >> p.score;
  65. file >> p.date;
  66. file.ignore();
  67. return p;
  68. }
  69.  
  70. void saveList()
  71. {
  72. ofstream ofs("h:\\Into to Programming\\hiscore.txt", ios::out);
  73. ofs << numPlayers << endl;
  74. for(int i=0; i<numPlayers; i++)
  75. sendtofile(ofs, playList[i]);
  76. ofs.close();
  77. }
  78.  
  79. void loadList()
  80. {
  81. try{
  82. ifstream ifs("h:\\Intro to Programming\\hiscore.txt", ios::in);
  83. ifs >> numPlayers;
  84. ifs.ignore();
  85. for(int i=0; i<numPlayers; i++)
  86. playList[i] = getfromfile(ifs);
  87. ifs.close();
  88. }
  89. catch(...){
  90. cout << "No data file.";
  91. }
  92. }
  93.  
  94. void printList()
  95. {
  96. for(int i=0; i<numPlayers; i++)
  97. showPlayer(playList[i]);
  98. }
  99.  
  100. void menu()
  101. {
  102. char choice;
  103. do{
  104. cout << "1) Load High Score Table." << endl;
  105. cout << "2) Save High Score Table." << endl;
  106. cout << "3) Add A High Score Entry." << endl;
  107. cout << "4) Display The High Score Table." << endl;
  108. cout << "5) Quit Program." << endl;
  109. cout << "Choose an option.";
  110. cin >> choice;
  111. cin.ignore();
  112. switch(choice)
  113. {
  114. case '1':
  115. loadList();
  116. break;
  117. case '2':
  118. saveList();
  119. break;
  120. case '3':
  121. getPlayer();
  122. numPlayers++;
  123. break;
  124. case '4':
  125. printList();
  126. break;
  127. default:
  128. ;
  129. }
  130. } while(choice!='5');
  131. }
  132.  
  133. void main()
  134. {
  135. loadList();
  136. menu();
  137. saveList();
  138.  
  139. }

i know the code is probably disastrous but i was hoping help could be given with what sense you can make out of it
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help With Making A High Scores Table

 
0
  #2
Dec 9th, 2008
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.
Last edited by Lerner; Dec 9th, 2008 at 12:55 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: gingo is an unknown quantity at this point 
Solved Threads: 0
gingo gingo is offline Offline
Newbie Poster

Re: Help With Making A High Scores Table

 
0
  #3
Dec 9th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help With Making A High Scores Table

 
0
  #4
Dec 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: gingo is an unknown quantity at this point 
Solved Threads: 0
gingo gingo is offline Offline
Newbie Poster

Re: Help With Making A High Scores Table

 
0
  #5
Dec 10th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC