Test scores average coming out incorrectly

Hey guys, I saw that someone posted a question about this program earlier but they are having a different issue than I am. When I compile the code and input the values, the average keeps returning as "-71582760.00," no matter what I put in.

After that, the program doesn't return a letter grade at all. I've worked on it for a while and also confirmed that the find average function was exactly like one I previously made in another program that worked properly. Thanks in advance for any 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
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 <iostream>
#include <iomanip>

using namespace std;

const	int MAXGRADE = 25;	// maximum number of grades per student 
const	int MAXCHAR = 30;	// maximum characters used in a name

typedef char StringType30[MAXCHAR + 1];	// character array data type for names
										// having 30 characters or less.

typedef	float GradeType[MAXGRADE];		// one dimensional integer array data type

float findGradeAvg(GradeType, int);		// finds grade average by taking array of
										// grades and number of grades as parameters

char findLetterGrade(float);			// finds letter grade from average given
										// to it as a parameter 
int main()
{
	StringType30 firstname, lastname;	// two arrays of characters defined
	int numOfGrades;					// holds the number of grades
	GradeType grades;					// grades defined as a one dimensional array
	float average;						// holds the average of a student's grade
	char moreInput;						// determines if there is more input
	char grade;

	cout << setprecision(2) << fixed << showpoint;

	// Input the number of grades for each student
	cout << "Please input the number of grades each student will receive." << endl
		<< "This must be a number between 1 and " << MAXGRADE << " inclusive"
		<< endl;
	cin >> numOfGrades;

	while (numOfGrades > MAXGRADE || numOfGrades < 1)
	{
		cout << "Please input the number of grades for each student." << endl
			<< "This must be a number between 1 and " << MAXGRADE
			<< " inclusive\n";
		cin >> numOfGrades;
	}

	// Input names and grades for each student
	cout << "Please input a y if you want to input more students"
		<< " any other character will stop the input" << endl;
	cin >> moreInput;

	while (moreInput == 'y' || moreInput == 'Y')
	{
		cout << "Please input the first name of the student" << endl;
		cin >> firstname;

		cout << endl << "Please input the last name of the student" << endl;
		cin >> lastname;

		for (int count = 0; count < numOfGrades; count++)
		{
			cout << endl << "Please input a grade" << endl;

			cin >> grades[0]; // Fill in the input statement to place grade in the array
		}

		cout << firstname << " " << lastname << " has an average of " ;

		average = findGradeAvg(grades, numOfGrades); // Fill in code to get and print average of student to screen
		cout << fixed << setprecision(2) << average << endl;

		grade = findLetterGrade(average);			 // Fill in call to get and print letter grade of student to screen
		

		cout << "This student's letter grade is " << grade << endl;

		cout << endl << endl << endl;

		cout << "Please input a y if you want to input more students"
			<< " any other character will stop the input" << endl;
		cin >> moreInput;
	}

	return 0;
}

//***********************************************************************
// findGradeAvg
//
// task:	      This function finds the average of the
//                numbers stored in an array.
//
// data in:	      an array of integer numbers
// data returned: the average of all numbers in the array
//
//***********************************************************************

float findGradeAvg(GradeType array, int numGrades)

{
	float sum = 0;
	
	    
	for (int count = 0; count < numGrades; count++)

	{
		sum += array[count];
	}

	return (sum / numGrades);

}

//***********************************************************************
// findLetterGrade
//
// task:	      This function finds the letter grade for the number
//                passed to it by the calling function
//
// data in:	      a floating point number
// data returned: the grade (based on a 10 point spread) based on the 
//                number passed to the function
//
//***********************************************************************

char findLetterGrade(float numGrade)
{
	char grade = 0;
	
	if (numGrade >= 90)
	{
		grade = 'A'; // Fill in the code for this function
	}
	else if (numGrade >= 80 && numGrade < 90)
	{
		grade = 'B';
	}
	else if (numGrade >= 70 && numGrade < 80)
	{
		grade = 'C';
	}
	else if (numGrade >= 60 && numGrade < 70)
	{
		grade = 'D';
	}
	if (numGrade < 60 && numGrade >= 0)
	{
		grade = 'F';
	}
	return grade;
}
1
2
3
4
5
6
		for (int count = 0; count < numOfGrades; count++)
		{
			cout << endl << "Please input a grade" << endl;

			cin >> grades[0]; // Fill in the input statement to place grade in the array
		}

What value is grades[1] set to here?

"GradeType" is a confusing typedef for an array.
"StringType30" encodes the magic number 30 into the name of the typedef itself.
If I were you I wouldn't use either of these typedefs, but maybe that's just me.
Last edited on
Yeah the GradeType and StringType30 were included in the skeleton code that we were provided. I don't mess with that stuff because I got docked some points once for modifying it.

Doesn't the grades input need to be written that way to accept user input into the array?
Your grades array is an array of 25 floats (although you don't necessarily use all these indices).
That is, grades[0] is a float, grades[1] is a float, ..., grades[24] is a float.

Your loop (lines 57 to 62) only ever puts user input into grades[0] (line 61).

You need to put values into grades indices beyond grades[0] as well.
Last edited on
Awesome, that did it. Thanks a lot, that was driving me insane!

Is there an explanation for why it kept returning the same number everytime? Like what was the program doing to keep getting that number?
Last edited on
The -71582760.00 number is just the result of using uninitialized values in your calculations. Essentially, junk values, and junk in = junk out.
Topic archived. No new replies allowed.