Grades calculator trouble

I have an assignment where I need to write a grades calculator for two tests, the tests average for each student, as well as the letter grade. What should I add to get the code to calculate the grades? The output window only shows one test graded even when the list I've put in the resource files has at least 10 names and test results. If someone knows how I can fix this problem any help would be appreciated thank you for your time.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148


#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <fstream> 
#include <string>
using namespace std;

fstream outFile;
double calculateAverage(double test1, double test2);
int calculateGrade(double avg);

int main()
{


	// This program calculates the overall total points for two tests, the numeric grade and the letter grade.
	//Declare variables to munipulate data
	string studentName;
	double test1, test2;
	double average = 0.00;
	char lettergrade;
	int studentCount = 0;
	double sumAverage = 0.00;
	double cAverage = 0.00;
	int i;

	string line;

	//Declare stream variables
	ifstream inFile;
	inFile.open("studentgrades.txt");
	string content;

	//Check for error
	if (inFile.fail()) {
		cerr << "Error opening file" << endl;
		exit(1);
	

	
	}

	ofstream outFile;
	outFile.open("k:\\Output.txt");
	cout << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Average" << setw(10) << "LetterGrade" << endl;
	outFile << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Average" << setw(10) << "LetterGrade" << endl;

	while (inFile.good())
	{
		//Open input file
		inFile >> studentName >> test1 >> test2;
		average = calculateAverage(test1, test2);
		char lettergrade = (average);
		i = 17 - studentName.size();

		cout << studentName << setw(i) << test1 << setw(10) << test2 << setw(10) << lettergrade << endl;
		outFile << studentName << setw(i) << test1 << setw(10) << test2 << setw(10) << average << setw(10) << lettergrade << endl;
		sumAverage = sumAverage + average;
		studentCount++;



		//Wait for a character before closing the console
		cout << "\nPress any key to coninue...";
		_getch();


	}


	//Calculate class average
	cAverage = sumAverage / studentCount;

	cout << setprecision(2) << fixed << showpoint << endl;
	cout << endl << "Class average is:" << cAverage << endl;
	outFile << setprecision(2) << fixed << showpoint << endl;
	outFile << endl << "Class average is:" << cAverage << endl;

	//Wait for a character before closing the console
	cout << "\nPress any key to coninue...";
	_getch();

}

//Calculate average of two tests
double calculateAverage(double test1, double test2)
{

	double avg;
	avg = (test1 + test2) / 2;
	return avg;


	int Num1, Num2, Max, Min, Sum;

	//Find sum
	Sum = Num1 + Num2;
	Max = Num1;
	if (Num2 > Max)
		Max = Num2;

	//Find Min
	Min = Num1;
	if (Num2 < Min)
		Min = Num2;
	// Sum divided by numbers Num1, Num2
	avg = (Sum) / 3.00;

	cout << "\n\nMaximum=" << Max;
	cout << "\n\nMinimum=" << Min;



}


//Calculate letter grade
int calculateGrade(double avg)

{


	char lettergrade;
	if (avg <= 100 && avg >= 90)
		lettergrade = 'A';
	else if (avg < 90 && avg >= 80)
		lettergrade = 'B';
	else if (avg < 70 && avg >= 60)
		lettergrade = 'C';
	else if (avg < 60 && avg >= 0)
		lettergrade = 'F';
	return lettergrade;


	cout << "Average:" << avg << endl;
	cout << "Letter Grade:"<< lettergrade << endl;


	//Wait for a character before closing the console
	cout << "\nPress any key to coninue...";
	_getch();

	return 0;

}
Last edited on
Please post a sample of your input file.

Also look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int calculateGrade(double avg)
{
	char lettergrade;
	if (avg <= 100 && avg >= 90)
		lettergrade = 'A';
	else if (avg < 90 && avg >= 80)
		lettergrade = 'B';
	else if (avg < 70 && avg >= 60)
		lettergrade = 'C';
	else if (avg < 60 && avg >= 0)
		lettergrade = 'F';

	return lettergrade;


	cout << "Average:" << avg << endl;


First do you understand what happens when a return statement is encountered?

Second your if()else if() chain could be simplified. You really shouldn't need both the upper and lower bounds in each statement one should suffice.
1
2
3
4
5
6
7
8
	if (avg >= 90)
		lettergrade = 'A';
	else if (avg >= 80)
		lettergrade = 'B';
	else if (avg >= 60)
		lettergrade = 'C';
	else
		lettergrade = 'F';


Here is my input file:
Anderson 90.5 80.75
Blake 75 90
Castillo 55 80
Dang 95 85
Engberg 80 100
Fu 55 90.5
Garcia 99 90
Hung 35 60
Iona 60.75 60
Johnson 65 40

So far " Anderson 90.5 80.75" with the student, test 1, test 2, letter grade columns above it are showing up in the output window. The average of both tests is being graded as U for some reason even after I changed the calculate grade section. Thank you for your help so far.
The average of both tests is being graded as U for some reason even after I changed the calculate grade section.

Look at line 56 in your last code, you need to call the function that computes the letter grade, not just print the "average" as a char.

I tried to work on it some more and it does build but now will not stay open.


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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <fstream> 
#include <string>
using namespace std;

fstream outFile;
double calculateAverage(double test1, double test2);
int calculateGrade(double avg);

int main()
{


	// This program calculates the overall total points for two tests, the numeric grade and the letter grade.
	//Declare variables to munipulate data
	string studentName;
	double test1, test2;
	double average = 0.00;
	char lettergrade;
	int studentCount = 0;
	double sumAverage = 0.00;
	double cAverage = 0.00;
	int i;

	string line;

	//Declare stream variables
	ifstream inFile;
	inFile.open("studentgrades.txt");
	string content;

	//Check for error
	if (inFile.fail()) {
		cerr << "Error opening file" << endl;
		exit(1);



	}

	ofstream outFile;
	outFile.open("studentgrades.txt");
	cout << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Average" << setw(10) << "LetterGrade" << endl;
	outFile << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Average" << setw(10) << "LetterGrade" << endl;

	while (inFile.good())
	{
		//Open input file
		inFile >> studentName >> test1 >> test2;
		average = calculateAverage(test1, test2);
		char lettergrade = (average);
		char average = (lettergrade);
		i = 17 - studentName.size();

		cout << studentName << setw(i) << test1 << setw(10) << test2 << setw(10) << lettergrade << endl;
		outFile << studentName << setw(i) << test1 << setw(10) << test2 << setw(10) << average << setw(10) << lettergrade << endl;
		sumAverage = sumAverage + average;
		studentCount++;



		//Wait for a character before closing the console
		cout << "\nPress any key to coninue...";
		_getch();


	}


	//Calculate class average
	cAverage = sumAverage / studentCount;

	cout << setprecision(2) << fixed << showpoint << endl;
	cout << endl << "Class average is:" << cAverage << endl;
	outFile << setprecision(2) << fixed << showpoint << endl;
	outFile << endl << "Class average is:" << cAverage << endl;

	//Wait for a character before closing the console
	cout << "\nPress any key to coninue...";
	_getch();

}

//Calculate average of two tests
double calculateAverage(double test1, double test2)
{

	double avg;
	avg = (test1 + test2) / 2;
	return avg;


	int Num1, Num2, Max, Min, Sum;

	//Find sum
	Sum = Num1 + Num2;
	Max = Num1;
	if (Num2 > Max)
		Max = Num2;

	//Find Min
	Min = Num1;
	if (Num2 < Min)
		Min = Num2;
	// Sum divided by numbers Num1, Num2
	avg = (Sum) / 3.00;

	cout << "\n\nMaximum=" << Max;
	cout << "\n\nMinimum=" << Min;



}


//Calculate letter grade
int calculateGrade(double avg)

{

		char lettergrade;
	if (avg >= 90)
		lettergrade = 'A';
	else if (avg >= 80)
		lettergrade = 'B';
	else if (avg >= 60)
		lettergrade = 'C';
	else
		lettergrade = 'F';
	return lettergrade;


	cout << "Average:" << avg << endl;
	cout << "Letter Grade:" << lettergrade << endl;


	//Wait for a character before closing the console
	cout << "\nPress any key to coninue...";
	_getch();

	return 0;

}
Topic archived. No new replies allowed.