Needing a little more help

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';
		}
	}
}
Last edited on
Why don't you you use a binary file? Honestly, they are simpler to use than text files for this sort of thing (IMO).

Also, why on earth would you use a 2D array for this? I don't see how you could, with what you're trying to do here.
It's funny how I can tell this is seeplus's code before even checking the post history :)

It's putting the score line itself (e.g. 8.4 9.4 8.1 ...) into the athletes_name variable, so that's why it's not printing the name correctly there.
Debug your code and look at the first instance where it puts unexpected data into the atheletes_name variable to see for yourself.

Problem is you're calling std::getline(scoreFile, line) twice; once in the for loop condition, and again in the "after every iteration" segment.

What happens if you change your for loop to
1
2
	
for (std::string athletes_name, line; std::getline(scoreFile, athletes_name) && std::getline(scoreFile, line);)
Last edited on
I'm sorry I just gave him credit for the changes he made. Sorry for the blunder. I appreciate the help @Ganado @seeplus. My teacher told me to use a 2D array and I don't know how I possibly could. @agent max
Currently you're not using a 2D array; you're (efficiently) only holding the two lines of memory you need at a time. Did your teacher say anything more specific? A 2D array to hold what? The grades?

If you want to hold the grades in an array, you'd do something like
1
2
3
constexpr size_t NUM_OF_STUDENTS {10};
constexpr size_t NUM_OF_SCORES {10};
double grades[NUM_OF_STUDENTS][NUM_OF_SCORES] { };


If you need to store the name side-by-side, create another simple array of strings.
std::string students[NUM_OF_STUDENTS];
(If you have learned structs or classes yet, you could combine some of these things into a single struct/class)

Then you'd have to assign each grades[i][j] and students[i] a value as you parse the file.

Why don't you you use a binary file? Honestly, they are simpler to use than text files for this sort of thing (IMO).
There is objectively no way that this assignment would be easier for a beginner using a binary file.
Last edited on
Yeah it's all become far too complicated for something to just plod through again testing as we go, and without all these cutesy convoluted, over-compounded and compacted lines.

A single line shouldn't exceed 80 characters - anything longer means it's unreadable junk, confuses an easy-to-read screen display, makes code unmanageable and a maintenance nightmare and probably doesn't work properly anyway.

(Formatting comes way after the number crunching is done. Spit out meaningful and correct output first. And mine might not be right I haven't checked :) )

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // SETUP ARRAYS
    int NO_ATHLETES{20};
    int NO_SCORES{10};

    string NAME[NO_ATHLETES];
    double RESULT[NO_ATHLETES][NO_SCORES];

    // SETUP FILE
    int count{0};

    ifstream data_file ("athletes.txt");
    
    // CLOSE IF FILE NOT OPENED
    if (!data_file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    
    // READ FILE CONTENTS
    while ( getline(data_file, NAME[count]))
    {
        for(int i = 0; i < 10; i++)
        {
            data_file >> RESULT[count][i];
        }
        // CLEAR STREAM FOR NEXT ATHLETE
        data_file.ignore();

        count++;
    }
    data_file.close(); // ALWAYS CLOSE FILES WHEN FINISHED WITH THEM

    double highest{0}, lowest{0}, total{0};
    // PROCESS SCORES
    for(int i = 0; i < count; i++)
    {
        cout << NAME[i] << '\n';
        // SCORE CALCULATIONS
        total = 0;
        highest = RESULT[i][0];
        lowest = RESULT[i][0];

        for(int j = 0; j < NO_SCORES; j++)
        {
            if(RESULT[i][j] > highest)
                highest = RESULT[i][j];

            if(RESULT[i][j] < lowest)
                lowest = RESULT[i][j];

            total += RESULT[i][j];
        }

        cout
        << "Highest: " << highest << ' '
        << "Lowest: " << lowest << ' '
        << "Average: " << (total - highest - lowest) / 8 << '\n';
    }
    
    return 0;
}



Mirabella Jones
Highest: 9.8 Lowest: 7 Average: 8.5875
Ruth Mendez
Highest: 9.8 Lowest: 6 Average: 8.0375
Melvin Ingram
Highest: 9.9 Lowest: 6 Average: 7.3125
Tara Silva
Highest: 9.5 Lowest: 6.4 Average: 7.8375
Joann Gardner
Highest: 9.1 Lowest: 6.2 Average: 7.8625
Jeff Barnes
Highest: 9.9 Lowest: 6 Average: 7.7125
Lucille Dixon
Highest: 9.7 Lowest: 6.2 Average: 8.6
Krista James
Highest: 9.9 Lowest: 6.1 Average: 8.5375
Naomi Sanders
Highest: 9.8 Lowest: 6.6 Average: 8.375
Ricky McCarthy
Highest: 9.8 Lowest: 6.2 Average: 8.1875
Susan Harper
Highest: 9.1 Lowest: 6 Average: 7.2875
Program ended with exit code: 0




Last edited on
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

class Athlete
{
   string name;
   vector<double> scores;
   double lowest, highest, sum;
   void stats();
public:
   bool isValid();
   friend istream & operator >> ( istream &in, Athlete &athlete );
   friend ostream & operator << ( ostream &out, const Athlete &athlete );
};


istream & operator >> ( istream &in, Athlete &a )
{
   string line;
   getline( in, a.name );
   getline( in, line );
   stringstream ss( line );
   a.scores.clear();
   for ( double value; ss >> value; ) a.scores.push_back( value );

   a.stats();
   return in;
}


ostream & operator << ( ostream &out, const Athlete &a )
{
   return out << setw( 20 ) << left << a.name
              << fixed << setprecision( 2 )
              << "    low = " << a.lowest
              << ",   high = " << a.highest
              << ",   av (ex. outliers) = " << ( a.sum - a.lowest - a.highest ) / ( a.scores.size() - 2 )
              << '\n';
}


void Athlete::stats()
{
   lowest  = *min_element( scores.begin(), scores.end() );
   highest = *max_element( scores.begin(), scores.end() );
   sum = accumulate( scores.begin(), scores.end(), 0.0 );
}


bool Athlete::isValid()
{
   return lowest >= 0.0 && highest <= 10.0 && scores.size() > 2;      // etc.
}


int main()
{
   ifstream in( "athletes.txt" );
   for ( Athlete a; in >> a; ) if ( a.isValid() ) cout << a;
}


Mirabella Jones         low = 7.00,   high = 9.80,   av (ex. outliers) = 8.59
Ruth Mendez             low = 6.00,   high = 9.80,   av (ex. outliers) = 8.04
Melvin Ingram           low = 6.00,   high = 9.90,   av (ex. outliers) = 7.31
Tara Silva              low = 6.40,   high = 9.50,   av (ex. outliers) = 7.84
Joann Gardner           low = 6.20,   high = 9.10,   av (ex. outliers) = 7.86
Jeff Barnes             low = 6.00,   high = 9.90,   av (ex. outliers) = 7.71
Lucille Dixon           low = 6.20,   high = 9.70,   av (ex. outliers) = 8.60
Krista James            low = 6.10,   high = 9.90,   av (ex. outliers) = 8.54
Naomi Sanders           low = 6.60,   high = 9.80,   av (ex. outliers) = 8.38
Ricky McCarthy          low = 6.20,   high = 9.80,   av (ex. outliers) = 8.19
Susan Harper            low = 6.00,   high = 9.10,   av (ex. outliers) = 7.29
the code not being able to read all of the lines in the text file.


Quite simple. The original data from the previous post had a blank line between the data for each person. The data presented here doesn't. If the format of the data changes, don't expect existing code to work. Changed data format usually means changes to the code to accommodate it.

For this format of data, just remove the final getline() from my L19. This was to originally skip over the blank line - which is now no longer needed.

Last edited on
@seeplus
As you would know, in the absence of allowing for both possibilities ...

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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    // SETUP ARRAYS
    int NO_ATHLETES{20};
    int NO_SCORES{10};

    std::string NAME[NO_ATHLETES];
    double RESULT[NO_ATHLETES][NO_SCORES];

    // SETUP FILE
    int count{0};
    std::string dummy;

    std::ifstream data_file ("athletes.txt");
    
    // CLOSE IF FILE NOT OPENED
    if (!data_file.is_open())
    {
        std::cout << "Unable to open file";
        return 0;
    }
    
    // READ FILE CONTENTS
    while ( getline(data_file, NAME[count]))
    {
        for(int i = 0; i < 10; i++)
        {
            data_file >> RESULT[count][i];
        }
        // CLEAR STREAM FOR NEXT ATHLETE
        data_file.ignore();

        getline(data_file, dummy);

        count++;
    }
    data_file.close();

    // PROCESS SCORES
    double highest{0}, lowest{0}, total{0};

    for(int i = 0; i < count; i++)
    {
        std::cout << NAME[i] << '\n';

        // SCORE CALCULATIONS
        total = 0;
        highest = RESULT[i][0];
        lowest = RESULT[i][0];

        for(int j = 0; j < NO_SCORES; j++)
        {
            if(RESULT[i][j] > highest)
                highest = RESULT[i][j];

            if(RESULT[i][j] < lowest)
                lowest = RESULT[i][j];

            total += RESULT[i][j];
        }

        std::cout
        << "Highest: " << highest << ' '
        << "Lowest: " << lowest << ' '
        << "Average: " << (total - highest - lowest) / (NO_SCORES - 2) << '\n';
    }
    
    return 0;
}


Mirabella Jones
Highest: 9.8 Lowest: 7 Average: 8.5875
Ruth Mendez
Highest: 9.8 Lowest: 6 Average: 8.0375
Melvin Ingram
Highest: 9.9 Lowest: 6 Average: 7.3125
Tara Silva
Highest: 9.5 Lowest: 6.4 Average: 7.8375
Joann Gardner
Highest: 9.1 Lowest: 6.2 Average: 7.8625
Jeff Barnes
Highest: 9.9 Lowest: 6 Average: 7.7125
Lucille Dixon
Highest: 9.7 Lowest: 6.2 Average: 8.6
Krista James
Highest: 9.9 Lowest: 6.1 Average: 8.5375
Naomi Sanders
Highest: 9.8 Lowest: 6.6 Average: 8.375
Ricky McCarthy
Highest: 9.8 Lowest: 6.2 Average: 8.1875
Susan Harper
Highest: 9.1 Lowest: 6 Average: 7.2875
Program ended with exit code: 0
athletes.txt in this case:
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

Topic archived. No new replies allowed.