I previously received some help with this code I am writing from seeplus, but I have ran into another error with the code not being able to read all of the lines in the text file. Also, my output for the invalid and disqualified athletes is not outputting there names just the scores. I was wondering how I would be able to fix this and also how I would use a 2D array in this instance.
Here is what's in the text file:
Mirabella Jones
7.5 8.8 7.0 8.1 8.0 9.8 9.3 8.9 9.1 9.0
Ruth Mendez
9.8 8.5 6.0 8.8 8.6 7.1 7.8 8.0 7.2 8.3
Melvin Ingram
9.9 7.3 6.3 7.0 6.8 6.2 8.9 9.5 6.5 6.0
Tara Silva
8.1 7.1 9.4 7.2 9.2 6.4 9.5 8.4 6.7 6.6
Joann Gardner
6.9 8.0 8.7 8.9 9.1 7.5 8.2 6.3 8.4 6.2
Jeff Barnes
6.4 7.2 8.3 8.6 7.9 6.0 7.1 6.7 9.5 9.9
Lucille Dixon
9.5 6.5 9.3 9.4 8.5 8.7 6.2 9.7 8.7 8.2
Krista James
8.4 9.4 8.1 6.3 6.1 8.6 9.6 9.1 9.9 8.8
Naomi Sanders
7.0 7.2 8.7 9.1 9.6 6.6 9.4 9.8 8.4 7.6
Ricky McCarthy
9.8 7.2 9.0 8.5 6.2 6.5 9.1 8.4 8.1 8.7
Susan Harper
6.0 7.1 8.8 9.1 6.6 6.3 6.1 8.0 6.9 8.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
int main() {
constexpr unsigned MIN_SCORE {};
constexpr unsigned MAX_SCORE {10};
constexpr size_t NUM_OF_SCORES {10};
const std::string SCORES_FILENAME = "scorecard1.txt";
// open the file with the test scores and check if successful
std::ifstream scoreFile(SCORES_FILENAME);
if (!scoreFile)
return (std::cout << "ERROR: the score file could not be opened\n"), 1;
for (std::string athletes_name, line; std::getline(scoreFile, athletes_name) && std::getline(scoreFile, line); std::getline(scoreFile, line)) {
double lowest_score {11.0}; // variable to store lowest score
double highest_score {}; // variable to store highest score
double total_score {}; // variable to store total score
double scores[NUM_OF_SCORES] {}; // Array to read the scores into
std::istringstream ilscores(line);
size_t no_score {};
bool disqual {};
for (; !disqual && no_score < NUM_OF_SCORES && ilscores >> scores[no_score]; ++no_score) {
if (scores[no_score] < MIN_SCORE || scores[no_score] > MAX_SCORE) {
std::cout << "Invalid scores. " << athletes_name << " is disqualified\n";
disqual = true;
}
total_score += scores[no_score];
if (scores[no_score] < lowest_score)
lowest_score = scores[no_score];
if (scores[no_score] > highest_score)
highest_score = scores[no_score];
}
if (!disqual && no_score != NUM_OF_SCORES)
std::cout << "\nInsufficient scores. \n" << athletes_name << " is disqualified\n";
else {
std::cout << '\n' << athletes_name << "\'s results:\n";
for (size_t s = 0; s < no_score; ++s)
std::cout << scores[s] << (s == no_score - 1 ? "\n" : ", ");
total_score -= highest_score + lowest_score; // exclude highest and lowest score
const auto average_score {total_score / 8.0}; // average calculation
std::cout << "The highest score of " << std::setprecision(3) << highest_score << " and the lowest score of " << std::setprecision(3) << lowest_score << " were dropped\n";
std::cout << "The average score is " << std::setprecision(3) << average_score << '\n';
}
}
}
| |