Grade System using arrays

I've been trying to create a grading system using arrays but i've been having issues when it outputs. it doesn't output all of the courses i entered and the corresponding grades. Here is what i have so far. I also have no idea how to add the grade letter as well. sigh

#include <iostream>
#include<iomanip>
using namespace std;
int numStudents2;

int main()
{
cout <<"Welcome to Jessie's Academy Grade Calculator" <<endl;
cout <<"Enter number of students: ";
cin >> numStudents2;

const int numStudents = numStudents2;
string studentName[numStudents];
int numCourses[numStudents];
string courseName[numStudents];
double courseGrade[numStudents];
float aveGrade[numStudents];
double sumGrades[numStudents];

for (int i = 0; i<numStudents; i++)
{
cout << "Name of Student: ";
cin >> studentName[i];
cout << "Number of courses taken: ";
cin >> numCourses[i];
for (int j = 0; j < numCourses[i]; j++)
{
cout << "Name of course " << j+1 << " taken: ";
cin >> courseName[j];
}
for (int l = 0; l < numCourses[i]; l++)
{cout << "Grade for course " << l+1 << ": ";
cin >> courseGrade[l];
}
sumGrades[i] += courseGrade[i];
aveGrade[i] = sumGrades[i] / numCourses[i];



}
cout<<"\n\n********Grade Report*********" <<endl;

cout <<"STUDENT NAME" <<"\t" <<"# OF COURSES" <<"\t" <<"COURSE NAME" <<"\t" <<"GRADE" <<"\t" <<"AVERAGE"<< endl;
for (int k = 0; k<numStudents; k++)
{

cout << studentName[k]<< "\t\t"<< numCourses[k] << "\t\t" << courseName[k] << "\t\t" << courseGrade[k] << "\t\t" << "\n";

for (int m= 0; m<numStudents; m++)
{
cout<<"\t\t\t\t" <<courseName[m] << "\t\t" << courseGrade[m] << "\t\t" << aveGrade[m] << "\n";
}
}


double max = aveGrade[0];
double min = aveGrade[0];
for (int i = 0; i < numStudents; i++)
{
if (max < aveGrade[i])
max = aveGrade[i];

if(min > aveGrade[i])
min = aveGrade[i];
}

cout<<"\n\nHighest Average: " << max;
cout<<"\nLowest Average: " << min;
return 0;
}


jessiej6 (1)
I've been trying to create a grading system using arrays but i've been having issues when it outputs. it doesn't output all of the courses i entered and the corresponding grades. Here is what i have so far. I also have no idea how to add the grade letter as well. sigh

#include <iostream>
#include<iomanip>
using namespace std;
int numStudents2;

int main()
{
cout <<"Welcome to Jessie's Academy Grade Calculator" <<endl;
cout <<"Enter number of students: ";
cin >> numStudents2;

const int numStudents = numStudents2;
string studentName[numStudents];
int numCourses[numStudents];
string courseName[numStudents];
double courseGrade[numStudents];
float aveGrade[numStudents];
double sumGrades[numStudents];

for (int i = 0; i<numStudents; i++)
{
cout << "Name of Student: ";
cin >> studentName[i];
cout << "Number of courses taken: ";
cin >> numCourses[i];
for (int j = 0; j < numCourses[i]; j++)
{
cout << "Name of course " << j+1 << " taken: ";
cin >> courseName[j];
}
for (int l = 0; l < numCourses[i]; l++)
{cout << "Grade for course " << l+1 << ": ";
cin >> courseGrade[l];
}
sumGrades[i] += courseGrade[i];
aveGrade[i] = sumGrades[i] / numCourses[i];



}
cout<<"\n\n********Grade Report*********" <<endl;

cout <<"STUDENT NAME" <<"\t" <<"# OF COURSES" <<"\t" <<"COURSE NAME" <<"\t" <<"GRADE" <<"\t" <<"AVERAGE"<< endl;
for (int k = 0; k<numStudents; k++)
{

cout << studentName[k]<< "\t\t"<< numCourses[k] << "\t\t" << courseName[k] << "\t\t" << courseGrade[k] << "\t\t" << "\n";

for (int m= 0; m<numStudents; m++)
{
cout<<"\t\t\t\t" <<courseName[m] << "\t\t" << courseGrade[m] << "\t\t" << aveGrade[m] << "\n";
}
}


double max = aveGrade[0];
double min = aveGrade[0];
for (int i = 0; i < numStudents; i++)
{
if (max < aveGrade[i])
max = aveGrade[i];

if(min > aveGrade[i])
min = aveGrade[i];
}

cout<<"\n\nHighest Average: " << max;
cout<<"\nLowest Average: " << min;
return 0;
}



c++ does not allow variable length arrays.
making a user input variable into a run time constant is not sufficient: it must be a COMPILE TIME constant.
that may or may not be the problem; many compilers tolerate the above and fix it for you (so an extended C++ syntax, there are a number of things like this that you can get away with that are strictly not correct).

if you want to fix that to see if it is causing some of the trouble, set numStudents to a reasonable constant like 5000 (large college freshman class??) or 100 (more than you will care to type into it while testing) or something and iterate over numStudents2 which is how many they actually want to use (ignore the rest of the array locations).

sum and average look totally wrong (in the wrong place). you are indexing[i] (out of range!) here -- the loop that modified i is done and i is now max+1...

min and max may collide with old macros on some compilers. there is a standard function std::min and std::max that replaced the macros but keep an eye on those as questionable variable names.

I don't see anything else. If those did not fix anything, use code tags to paste your updated code with indention and can look again. <> on the side bar or code and /code blocks in [] brackets will allow syntax and indented code.
Last edited on
There seems to be an issue re storing multiple grades for the different students. A 2 dimensional array is needed. Consider:

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

constexpr size_t maxStuds {100};
constexpr size_t maxCrse {10};

int main() {
	size_t numStudents {};

	cout << "Welcome to Jessie's Academy Grade Calculator\n";
	cout << "Enter number of students: ";
	cin >> numStudents;

	numStudents = std::min(maxStuds, numStudents);

	string studentName[maxStuds];
	size_t numCourses[maxStuds] {};
	string courseName[maxCrse][maxStuds];
	double courseGrade[maxCrse][maxStuds] {};
	double aveGrade[maxStuds] {};
	double sumGrades[maxStuds] {};

	for (size_t i = 0; i < numStudents; ++i) {
		cout << "Name of Student " << i + 1 << ": ";
		cin >> studentName[i];

		cout << "Number of courses taken: ";
		cin >> numCourses[i];

		numCourses[i] = std::min(numCourses[i], maxCrse);

		for (size_t j = 0; j < numCourses[i]; ++j) {
			cout << "Name of course " << j + 1 << " taken: ";
			cin >> courseName[j][i];
		}

		for (int l = 0; l < numCourses[i]; ++l) {
			cout << "Grade for course " << l + 1 << ": ";
			cin >> courseGrade[l][i];
			sumGrades[i] += courseGrade[l][i];
		}

		aveGrade[i] = sumGrades[i] / numCourses[i];
	}

	double maxgr {aveGrade[0]};
	double mingr {aveGrade[0]};

	cout << "\n\n********Grade Report*********\n";
	cout << "STUDENT NAME" << "\t" << "# OF COURSES" << "\t" << "AVERAGE" << "\t\t" << "COURSE NAME" << "\t" << "GRADE\n";

	for (size_t k = 0; k < numStudents; ++k) {
		cout << studentName[k] << "\t\t" << numCourses[k] << "\t\t" << aveGrade[k] << "\t\t"; // << "\t\t" << courseName[k] << "\t\t" << courseGrade[k] << "\t\t" << "\n";

		for (size_t m = 0; m < numCourses[k]; ++m) {
			if (m)
				cout << "\t\t\t\t\t\t";

			cout << courseName[m][k] << "\t\t" << courseGrade[m][k] << "\n";
		}

		cout << '\n';

		if (maxgr < aveGrade[k])
			maxgr = aveGrade[k];

		if (mingr > aveGrade[k])
			mingr = aveGrade[k];
	}

	cout << "\n\nHighest Average: " << maxgr;
	cout << "\nLowest Average: " << mingr;
}



Welcome to Jessie's Academy Grade Calculator
Enter number of students: 3
Name of Student 1: qwe
Number of courses taken: 2
Name of course 1 taken: po
Name of course 2 taken: lk
Grade for course 1: 8
Grade for course 2: 9
Name of Student 2: asd
Number of courses taken: 3
Name of course 1 taken: kj
Name of course 2 taken: hg
Name of course 3 taken: mn
Grade for course 1: 6
Grade for course 2: 5
Grade for course 3: 4
Name of Student 3: zxc
Number of courses taken: 2
Name of course 1 taken: hg
Name of course 2 taken: bv
Grade for course 1: 4
Grade for course 2: 5


********Grade Report*********
STUDENT NAME    # OF COURSES    AVERAGE         COURSE NAME     GRADE
qwe             2               8.5             po              8
                                                lk              9

asd             3               5               kj              6
                                                hg              5
                                                mn              4

zxc             2               4.5             hg              4
                                                bv              5



Highest Average: 8.5
Lowest Average: 4.5

Topic archived. No new replies allowed.