due Tuesday, 1/26 1:30 pm
The purpose of this assignment is to maintain a list of 10 highest scores for some game. The assignment requires building and using two libraries and reading and writing a binary file. The program will make use of an input file containing game scores.
Wilma Flintstone 02/28/2020 16 Fred Flintstone 29Feb16 57 Lady Gaga 08Aug15 34 Donald J Trump 42 Cool Hand Luke 06Aug14 59 Harry S Truman 10/19/16 52 Fred Flintstone 02/28/2020 50 Wilma Flintstone 28Feb20 10 Obama 30 Clyde 09/11/17 98 ... |
int main() { Score scores[MaxNumberOfScores]; ifstream fin(InputFile); if (!fin) { cerr << "Unable to open input file " << InputFile << endl; exit(1); } Score newScore; unsigned numScores; // number of scores to be processed bool updateScores = false; // flag to indicate a change in the scores array eraseScoresFile(ScoresFile); // remove existing binary scores file while (processInputFile(fin, newScore)) // read a record from input file { // read binary Scores file numScores = getScoresFromFile(ScoresFile, scores); if (numScores < MaxNumberOfScores) // Less than 10 Scores { scores[numScores++] = newScore; updateScores = true; } else if (scores[numScores - 1] < newScore) // newScore > lowest of Top Scores { scores[numScores - 1] = newScore; // add newScore to Top Scores updateScores = true; } else updateScores = false; // no change to scores array if (updateScores) { sort(scores, numScores); // sort Scores for (auto i = 0U; i < numScores; ++i) cout << left << setw(3) << i+1 << scores[i] << endl; cout << "----------------------------------\n"; // Write binary Scores file writeScoresToFile(ScoresFile, scores, numScores); } } } |
1 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Fred Flintstone 57 02/29/2016 2 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Fred Flintstone 57 02/29/2016 2 Lady Gaga 34 08/08/2015 3 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Fred Flintstone 57 02/29/2016 2 Donald J Trump 42 12/19/2020 3 Lady Gaga 34 08/08/2015 4 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Cool Hand Luke 59 08/06/2014 2 Fred Flintstone 57 02/29/2016 3 Donald J Trump 42 12/19/2020 4 Lady Gaga 34 08/08/2015 5 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Cool Hand Luke 59 08/06/2014 2 Fred Flintstone 57 02/29/2016 3 Harry S Truman 52 10/19/2016 4 Donald J Trump 42 12/19/2020 5 Lady Gaga 34 08/08/2015 6 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Cool Hand Luke 59 08/06/2014 2 Fred Flintstone 57 02/29/2016 3 Harry S Truman 52 10/19/2016 4 Fred Flintstone 50 02/28/2020 5 Donald J Trump 42 12/19/2020 6 Lady Gaga 34 08/08/2015 7 Wilma Flintstone 16 02/28/2020 ------------------------------------ 1 Cool Hand Luke 59 08/06/2014 2 Fred Flintstone 57 02/29/2016 3 Harry S Truman 52 10/19/2016 4 Fred Flintstone 50 02/28/2020 5 Donald J Trump 42 12/19/2020 6 Lady Gaga 34 08/08/2015 ... 1 Clyde 98 09/11/2017 2 Bonnie 96 04/25/2019 3 Donald J Trump 67 05/11/2013 4 Cool Hand Luke 59 08/06/2014 5 Fred Flintstone 57 02/29/2016 6 Fred Flintstone 55 02/28/2016 7 Harry S Truman 52 10/19/2016 8 Fred Flintstone 50 02/28/2020 9 Fred Flintstone 50 02/29/2020 10 Bonnie 50 09/15/2013 ------------------------------------ 1 Clyde 98 09/11/2017 2 Bonnie 96 04/25/2019 3 Bonnie 77 05/08/2013 4 Donald J Trump 67 05/11/2013 5 Cool Hand Luke 59 08/06/2014 6 Fred Flintstone 57 02/29/2016 7 Fred Flintstone 55 02/28/2016 8 Harry S Truman 52 10/19/2016 9 Fred Flintstone 50 02/28/2020 10 Fred Flintstone 50 02/29/2020 ------------------------------------ 1 Clyde 98 09/11/2017 2 Bonnie 96 04/25/2019 3 Bonnie 77 05/08/2013 4 Donald J Trump 67 05/11/2013 5 Cool Hand Luke 59 08/06/2014 6 Fred Flintstone 57 02/29/2016 7 Fred Flintstone 55 02/28/2016 8 Harry S Truman 52 10/19/2016 9 Lady Gaga 52 08/09/2019 10 Fred Flintstone 50 02/28/2020 ------------------------------------ 1 Clyde 98 09/11/2017 2 Bonnie 96 04/25/2019 3 Bonnie 77 05/08/2013 4 Shirley Temple 69 10/18/2018 5 Donald J Trump 67 05/11/2013 6 Cool Hand Luke 59 08/06/2014 7 Fred Flintstone 57 02/29/2016 8 Fred Flintstone 55 02/28/2016 9 Harry S Truman 52 10/19/2016 10 Lady Gaga 52 08/09/2019 ------------------------------------ 1 Clyde 98 09/11/2017 2 Bonnie 96 04/25/2019 3 Bonnie 77 05/08/2013 4 Shirley Temple 69 10/18/2018 5 Donald J Trump 67 05/11/2013 6 R2D2 64 12/19/2020 7 Cool Hand Luke 59 08/06/2014 8 Fred Flintstone 57 02/29/2016 9 Fred Flintstone 55 02/28/2016 10 Harry S Truman 52 10/19/2016 ------------------------------------ |
In my solution, I used 3 functions to communicate with the binary file: * eraseScoresFile - erases an existing binary scores file. This is called only one time. * getScoresFromFile - reads (all) scores data from the binary file * writeScoresToFile - writes (all) scores data back into the binary file In the "processing loop" (which is meant to simulate game playing history), the binary file is first read and processed (even if empty) and then one new score is read (from the "input file") and it may (usually) get added to the high scores data. Then the high scores data is written back out to the binary file. All of this happens each time in the "processing" loop. It is not efficient, because of all the file I/O, but it works well & solves the problem. You are not required to follow my approach, nor use my main. But you are required to accomplished the transactions that I described and produce the output shown. |