What's wrong with my code?

The objective of this program is to ask the user for the name of a data file.
If the file does not open, exit the program.
This data file contains a list of students in a class.
The names are listed, one per line, in the following format:

lastName firstName middleInitial

and each part of the name is separated by a space.

Each student in this class has a data file that contains an unknown number of test scores. The name of each student’s data file is formed by concatenating the students first and last name and adding “.dat”.

I believe that I have that part works. This is the part that I am having issues with:

Your program should open each student’s data file, read in the scores and calculate the student’s average.
In addition to calculating the average for each student, you should report the average for the class, and the highest and lowest test score in the class.

Other Requirements

If the student file fails to open - print the error message "No Data File".
If there are no grades in the file - print the message "No Grades".
This is the code that I have so far, I could really use some help. I just can't get it to run correctly it's giving me a return of 1.

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
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;

// Driver program to test
int main()
{
    string dataFileName;
    string lastname, firstname, middleIntial, student_file, fullname;
    double min_score;
    double max_score;
    double total_score = 0;
    int student_count = 0;
    int count = 0;
    double score = 0;

    cout<<"Please Enter the Name of a Data File: ";
    cin>>dataFileName;
    cout<<dataFileName<<endl;
    ifstream openDataFile(dataFileName);



    if(!openDataFile.is_open())
    {
        cout<<"Error: File Not Open\n";
        exit(EXIT_FAILURE);

    }

    cout << left << setw(30) << "Students"      <<        "Average" << endl;


    while( openDataFile >> lastname >> firstname >> middleIntial)
    {
        fullname = firstname + " " + middleIntial + " " + lastname;
        student_file = firstname + lastname + ".dat";

        // Opening student file for reading test scores
        ifstream s_file( student_file );
        int temp;
        while( s_file >> temp)
        {
            score += temp;
            if ( temp < min_score )
                min_score = temp;
            else if ( temp > max_score )
                max_score = temp;
            count++;
        }

        double average = score / count ;
        cout << left << setw(30)
            << fullname << fixed << setprecision(2)
                << average << endl;

        total_score += average;
        student_count++;

        s_file.close();
    }

    double class_avg = total_score /  student_count;

    cout << left << setw(30) <<"Class Average: " <<fixed << setprecision(2) << class_avg << endl;
    cout << left << setw(30) <<"Max Score: " <<   fixed << setprecision(2) << max_score << endl;
    cout << left << setw(30) <<"Min Score: " <<   fixed << setprecision(2) << min_score << endl;

    openDataFile.close() ;

    return 0;

}


This is a sample txt file:
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

Data File: Students.dat

Tuel Dennis D

Drummond Ann M

Maxwell Virginia L

DennisTuel.dat

85

88

92

86

AnnDrummond.dat

95

72

88

89

VirginiaMaxwell.dat

68

75

83

98


this is what my output This is the output I get. to me it seems like it's reading the file, and printing it not processing the .dat info:

1
2
3
4
5
6
7
8
9
10
11
12
13
Students                      Average
Dennis D Tuel                 nan
Ann M Drummond                nan
Virginia L Maxwell            nan
85 88 DennisTuel.dat          nan
86 AnnDrummond.dat 92         nan
72 88 95                      nan
VirginiaMaxwell.dat 68 89     nan
83 98 75                      nan
Class Average:                nan
Max Score:                    0.00
Min Score:                    0.00


this is what it is supposed to be:

1
2
3
4
5
6
7
8
9
10
11
Enter Name of Data File: Students.dat <enter>


Students.                  Average
Dennis D Tuel.           87.75
Ann M Drummond.     86.00
Virginia L Maxwell.     81.00
Class Average:           84.92
Max Score:                98.00
Min Score:                 68.00





Last edited on
Something seems to have removed every <rest_of_the_line from your posted code (notice how the #include s are missing something, and some of your cout <<s are truncated). Could you edit your post and try uploading your code again?

-Albatross
thanks, I didn't notice that. I updated my code, so it post that one instead thanks again.
Something is wrong here.

Is this sample data setup in a single file meant to be serious?

A separate file with a list of names and then separate .dat files associated with each on individually would make more sense however cumbersome that might be.
Seems to work for me.

$ for i in *.dat ; do echo $i ; echo "-----" ; cat $i ; echo ===== ; done
AnnDrummond.dat
-----
95
72
88
89
=====
DennisTuel.dat
-----
85
88
92
86
=====
Students.dat
-----
Tuel Dennis D
Drummond Ann M
Maxwell Virginia L
=====
VirginiaMaxwell.dat
-----
68
75
83
98
=====
$ ./a.out 
Please Enter the Name of a Data File: Students.dat
Students.dat
Students                      Average
Dennis D Tuel                 87.75
Ann M Drummond                86.88
Virginia L Maxwell            84.92
Class Average:                86.51
Max Score:                    98.00
Min Score:                    0.00


Well mostly, you need to initialise min_score and max_score properly.
$ for i in *.dat ; do echo $i ; echo "-----" ; cat $i ; echo ===== ; done
Excellent. There was nothing to say it had to be C++.
Perhaps (not tried):

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

int main() {
	std::string dataFileName;
	double min_score {};
	double max_score {};
	double total_score {};
	size_t student_count {};

	std::cout << "Please Enter the Name of a Data File: ";
	std::cin >> dataFileName;

	std::ifstream openDataFile(dataFileName);

	if (!openDataFile)
		return (std::cout << "Error: File Not Open\n"), EXIT_FAILURE;

	std::cout << std::left << std::setw(30) << "Students" << "Average\n";

	for (std::string lastname, firstname, middleInitial; openDataFile >> lastname >> firstname >> middleInitial; ) {
		const auto fullname {firstname + " " + middleInitial + " " + lastname};
		const auto student_file {firstname + lastname + ".dat"};

		// Opening student file for reading test scores
		std::ifstream s_file(student_file);

		if (s_file) {
			size_t count {};
			double score {};

			for (double temp; s_file >> temp; score += temp, ++count) {
				if (temp < min_score)
					min_score = temp;

				if (temp > max_score)
					max_score = temp;
			}

			const double average {score / count};

			std::cout << std::left << std::setw(30)
				<< fullname << std::fixed << std::setprecision(2)
				<< average << '\n';

			total_score += average;
			++student_count;
		} else
			std::cout << "Cannot find file for student " << student_file << '\n';
	}

	const auto class_avg {total_score / student_count};

	std::cout << std::left << std::setw(30) << "Class Average: " << std::fixed << std::setprecision(2) << class_avg << '\n';
	std::cout << std::left << std::setw(30) << "Max Score: " << std::fixed << std::setprecision(2) << max_score << '\n';
	std::cout << std::left << std::setw(30) << "Min Score: " << std::fixed << std::setprecision(2) << min_score << '\n';
}


Topic archived. No new replies allowed.