Needing help modifying my code.

I was wondering what would be the best way from me to make my code read the data from multiple athletes and output it. What changes would I need would I need to make to my arrays?

Original input file:
Mirabella Jones
7.5 8.8 7 8.1 8 9.8 9.3 8.9 9.1 9

New input file:
Ronald Jones
7.5 8.8 7 8.1 8 9.8 9.3 8.9 9.1 9

Mirabella Jones
7.5 8.8 7 8.1 8 9.8 9.3 8.9 9.1 9

Ryan Sheckler
7.5 8.8 7 8.1 8 9.8 9.3 8.9 9.1 9


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  C++ Source Code:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {
    // constants
const int MIN_SCORE = 0;
const int MAX_SCORE = 10;
const int NUM_OF_SCORES = 10;  // num of scores
const string SCORES_FILENAME = "scores.txt"; //file name

string athletes_fname = ""; // variable to store athlete's name
string athletes_lname = ""; // variable to store athlete's name
double lowest_score = 11.0;  // varibale to store lowest score
double highest_score = 0.0; // variable to store highest score
double average_score = 0.0; // variable to store average score
double total_score = 0.0; // variable to store total score score

    // open the file with the test scores and check if successful
    ifstream scoreFile;
    scoreFile.open(SCORES_FILENAME);
    if (scoreFile) {
        double scores[NUM_OF_SCORES] = {0.0f}; // Array to read the scores into
        string error = "";

        scoreFile >> athletes_fname;  // read athlete's first name from file
        scoreFile >> athletes_lname;  // read athlete's last name from file

     

        for (int i = 0; i < NUM_OF_SCORES; ++i) {
            // check if we ran into EOF
            if (!(scoreFile >> scores[i])) {
                error = "Insufficient scores\n"+athletes_fname +" "+athletes_lname+" is disqualified";
                break;
            }
            // check if the score we read is valid
            if (scores[i] < MIN_SCORE || scores[i] > MAX_SCORE) {
                 error = "Invalid scores\n"+athletes_fname +" "+athletes_lname+" is disqualified";
                break;
            }

        }
        
        // if no errors
        if (error == "") {

            cout<<athletes_fname <<" "+athletes_lname+"\'s results: "<<endl;
            // Range-based for loop
            for (double score : scores) {
                if(score == scores[9])
                    cout<<score;
                else
                cout<<score<<", ";
               total_score +=score;

                // check and store lowest score
            if(score < lowest_score ){
                lowest_score = score;
            }

            // check and store highest score
             if(score > highest_score ){
                highest_score = score;
            }
          
            }

            total_score = total_score - (highest_score + lowest_score); // exclude highest and lowest score
            average_score = (total_score / 8);  // average calculation

            // display results
            cout << endl;
            cout << "The highest score of "<<setprecision(3)<<highest_score <<" and the lowest score of "<<setprecision(3)<<lowest_score <<" were dropped"<<endl;

            cout << "The average score is "<<setprecision(3)<<average_score<<endl;

        } else {
            cout << error << endl;
        }
    } else {
        cout << "ERROR: the score file could not be opened";
    }
return 0;
Hello akeilo,

To start with consider:
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
using namespace std;  // <--- Best not to use.

int main()
{
    // constants
    const int MIN_SCORE{};
    const int MAX_SCORE{ 10 };
    const int NUM_OF_SCORES{ 10 };  // num of scores
    const string SCORES_FILENAME{ "scores.txt" }; //file name
    
    std::ifstream scoreFile(SCORES_FILENAME);

    if (!scoreFile)
    {
        std::cout << "\n     File " << std::quoted(SCORES_FILENAME) << " did not open.\n";  // <--- Requires header file <iomanip>.
        //std::cout << "\n     File \"" << SCORES_FILENAME << "\" did not open.\n";

        return 1;
    }

    string athletes_fname; // variable to store athlete's name  // <--- Empty when defined. Does not need initialized.
    string athletes_lname; // variable to store athlete's name
    double lowest_score{ 11.0 };  // varibale to store lowest score
    double highest_score{ 0.0 }; // variable to store highest score
    double average_score{ 0.0 }; // variable to store average score
    double total_score{ 0.0 }; // variable to store total score score 


Line 11 defines the stream variable and opens the file.

This is followed by the check. If the open worked you bypass the if statement and continue with the program. In not you leave the program to fix the problem.

The comment on line 21 also applies to the other container classed like vector, list plus others. They are all empty with (0)zero size until you put something in them.

Saying std::string athletes_fname = ""; has no effect on the variable it is empty with a size of (0)zero.

Line 26, of your code, is not the best way to run the program. And by checking if the file is open and usable early it is now unnecessary.

Line 27 and 28, of your code, are defined inside the {}s of the if statement. This may work for now, but at the closing brace of the if statement the local variables are destroyed, so if you would need these later they are no longer available, bot the code only executes 1 time, so it does not matter how many players in in the input file you only deal with the first 1.

You would need to replace the if/else with a while loop where the condition reads the file for at least first name or first and last names. When it fails to read anything and sets the "eof" bit on the stream the condition will fail and you will be done.

Andy
code read the data from multiple athletes and output it

For one way, consider:

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
#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 = "scores.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 << "Insufficient scores. " << 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';
		}
	}
}



Ronald Jones's results:
7.5, 8.8, 7, 8.1, 8, 9.8, 9.3, 8.9, 9.1, 9
The highest score of 9.8 and the lowest score of 7 were dropped
The average score is 8.59

Mirabella Jones's results:
7.5, 8.8, 7, 8.1, 8, 9.8, 9.3, 8.9, 9.1, 9
The highest score of 9.8 and the lowest score of 7 were dropped
The average score is 8.59

Ryan Sheckler's results:
7.5, 8.8, 7, 8.1, 8, 9.8, 9.3, 8.9, 9.1, 9
The highest score of 9.8 and the lowest score of 7 were dropped
The average score is 8.59


Note that the original file data is the same for all!
Last edited on
Hello akeilo,

Referencing your code I changed the if statement in line 26 to:
while (scoreFile >> athletes_fname && scoreFile >> athletes_lname) and deleted the else part of the if statement.

That makes lines 30 and 31 unnecessary.

The only other change I made is on line 50. I changed if (error == "") to if (error.empty()).

The rest of your code appears to work, not that I have completely checked it.

The rest of the changes were small like changing the "endl"s to new line characters, (\n). Some of these can be put at the end of the string for the others I just replaced "end" with (\n);

The last was to adjust the output and I get this:

Ronald Jones's results:
7.5, 8.8, 7, 8.1, 8, 9.8, 9.3, 8.9, 9.1, 9

The highest score of 9.8 and the lowest score of 7 were dropped
The average score is 8.59

Mirabella Jones's results:
7.5, 8.8, 7, 8.1, 8, 9.8, 9.3, 8.9, 9.1, 9

The highest score of 9.8 and the lowest score of 7 were dropped
The average score is 17.2

Ryan Sheckler's results:
7.5, 8.8, 7, 8.1, 8, 9.8, 9.3, 8.9, 9.1, 9

The highest score of 9.8 and the lowest score of 7 were dropped
The average score is 25.8


The output is up to you, but I would suggest putting a blank line before the 2nd and 3rd name.

Andy
Topic archived. No new replies allowed.