so i have a simple trivia game as my final project in my c++ class. as it is, i would get an A+ already, but i want to be able to have top 5 high scores. I know how to read from a .dat file, and write, and i know what is supposed to happen for what i want, I just cant manage to type it out properly.

so far, I have it printing the player's name and score, along with a grade. after that, I want it to print the top 5 scores(kept in a file) then check if the player's score is higher than any of them. then add in their score to the correct placement(between next highest, and lowest). after which, it will rewrite back to the file, so next time another player will have the same options.

could someone please help my with my coder's block?

9 Years
Discussion Span
Last Post by Duoas

All you need is an array or vector of items, say 't_top_score', which you read from file, modify, then write back to file.

struct t_top_score
  string name;
  int score;

  friend ostream& operator << ( ostream& outs, const t_top_score& top_score );
  friend istream& operator >> ( istream& ins, t_top_score& top_score );

ostream& operator << ( ostream& outs, const t_top_score& top_score )
  outs << top_score.name << ' ' << top_score.score << endl;
  return outs;


vector<t_top_score> top_scores( 5 );

for (int i = 0; i < top_scores.size(); i++)
  cout << top_scores[ i ];


Good luck.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.