Computing Course Grade

Write a program to compute numeric grades for a course. The course records are in a file called "inputGrades.txt” that will serve as the input file. The input file is in exactly the following format: The first line in the input file contains the student number. The next line contains 14 lab scores separated by spaces, then the next line contains 7 assignment scores separated by spaces, then the next line contains 2 exam scores separated by spaces,
and finally, the last line contains a single final exam score. The following is an example of a single line from the input file:

1807453
95 85 95 85 85 92 65 76 67 94 34 67 88 100
88 78 93 92 100 76 84
88 75
85

Your program will take its input from this input file and send its output to a second output file called “outputGrades.txt”. The data in the output file will be the same as the data in the input file, except that there will be one additional number (of type double) at the end of each line. This number will be the overall student's grade rounded to two decimal places. For the purposes of the overall grade calculations, labs are worth 15%, assignments are worth 25%, each exam is worth 20%, and the final is worth 20% of the student's grade.


Having trouble with this program. Can anyone guide me in the right direction?

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

void calculate(ifstream& input_file, ofstream& output_file);

int main() {

	ifstream input_file;
	ofstream output_file;

	input_file.open("inputGrades.txt");

	if (input_file.fail()) {
		cout << "Unable to open the input file\n";
		exit(0);
	}

	output_file.open("outputGrades.txt");

	if (output_file.fail()) {
		cout << "Unable to open the output file\n";
		exit(0);
	}

	calculate(input_file, output_file);

	input_file.close();
	output_file.close();

	cout << "Done reading and writing files.\n";

	cout << endl;

	return 0;
}

void calculate(ifstream& input_file, ofstream& output_file) {
	
	int student_number;
	double lab_sum = 0;
	double lab_grade;
	double asssignment_sum = 0;
	double assignment_grade;
	double test_sum = 0;
	double test_grade;
	double exam_sum = 0;
	double exam_grade;
	double course_grade;

	cin >> student_number;

	for (int i = 0; i < 14; i++) {
		cin >> lab_grade;
		lab_sum += lab_grade;
	}

	for (int j = 0; j < 7; j++) {
		cin >> assignment_grade;
		asssignment_sum += assignment_grade;
	}
	
	for (int k = 0; k < 2; k++) {
		cin >> test_grade;
		test_sum += test_grade;
	}
	
	for (int m = 0; m < 1; m++) {
		cin >> exam_grade;
		exam_sum += exam_grade;
	}

	

	course_grade = ((lab_sum / 14) * 0.15) + ((asssignment_sum / 7) * 0.25) + ((test_sum / 2) * 0.4) + ((exam_sum / 1) * 0.2);

	output_file << "The course grade is " << course_grade;
	cout << "The course grade is " << course_grade << endl;
}
Last edited on
Describe your trouble so that we can help
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
#include <iostream>
#include <fstream>
using namespace std;

void calculate(ifstream& input_file, ofstream& output_file);

int main() {

	ifstream input_file;
	ofstream output_file;

	input_file.open("inputGrades.txt");

	if (input_file.fail()) {
		cout << "Unable to open the input file\n";
		exit(0);
	}

	output_file.open("outputGrades.txt", ios::app);

	if (output_file.fail()) {
		cout << "Unable to open the output file\n";
		exit(0);
	}

	calculate(input_file, output_file);

	input_file.close();
	output_file.close();

	cout << "\nDone reading and writing files.\n";

	cout << endl;

	return 0;
}

void calculate(ifstream& input_file, ofstream& output_file) {
	
	double lab_sum = 0;
	double lab_grade;
	double asssignment_sum = 0;
	double assignment_grade;
	double test_sum = 0;
	double test_grade;
	double exam_sum = 0;
	double exam_grade;
	double course_grade;
	int count = 0;

	while (!input_file.eof()) {

		if (count >= 1 && count <= 14) {
			input_file >> lab_grade;
			lab_sum += lab_grade;
		}
		if (count >= 15 && count <= 21) {
			input_file >> assignment_grade;
			asssignment_sum += assignment_grade;
		}
		if (count >= 22 && count <= 23) {
			input_file >> test_grade;
			test_sum += test_grade;
		}
		if (count = 24) {
			input_file >> exam_grade;
			exam_sum += exam_grade;
		}
		count++;
	}

	course_grade = ((lab_sum / 14) * 0.15) + ((asssignment_sum / 7) * 0.25) + ((test_sum / 2) * 0.4) + ((exam_sum / 1) * 0.2);

	output_file << "The course grade is " << course_grade;
	cout << "The course grade is " << course_grade << endl;
}
Updated code above but the final course grade is way off. I'm having trouble separating the different types of grades correctly.
Quickly looking at the updated program if (count = 24) { should be if (count == 24) { also if there is no action for count == 0 then why not initialize count to 1 directly?
I am trying to skip over the student id number so that it is not included in the calculation
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
void calculate(ifstream& input_file, ofstream& output_file) {
	
	double lab_sum = 0;
	double lab_grade;
	double asssignment_sum = 0;
	double assignment_grade;
	double test_sum = 0;
	double test_grade;
	double exam_sum = 0;
	double exam_grade;
	double course_grade;
	int count = 0;

	while (count <= 24) {
		if (count >= 1 && count <= 14) {
			input_file >> lab_grade;
			lab_sum += lab_grade;
		}
		if (count >= 15 && count <= 21) {
			input_file >> assignment_grade;
			asssignment_sum += assignment_grade;
		}
		if (count >= 22 && count <= 23) {
			input_file >> test_grade;
			test_sum += test_grade;
		}
		if (count == 24) {
			input_file >> exam_grade;
			exam_sum += exam_grade;
		}
		count++;
	}

	course_grade = ((lab_sum / 14) * 0.15) + ((asssignment_sum / 7) * 0.25) + ((test_sum / 2) * 0.4) + ((exam_sum / 1) * 0.2);

	output_file << "The course grade is " << course_grade;
	cout << "The course grade is " << course_grade << endl;
}
the above worked correctly once I deleted the student id from the .txt file. just need to figure out how to make it work with it included
Read the student ID and dont use it.

1
2
3
long stid = 0;
...
input_file >> stid;

before starting to read grades
worked. thank you!
Topic archived. No new replies allowed.