Grade array help needed!

Pages: 12
Heyhey!
Need some help with my array for printing nad calculating grades. So the task is to build a program with three arrays; Courses, score and grades, and also to make a class of StudentGrades and a function to print each objects

My first issue is that I get the error that all my objects were not declared in this scope. How do I fix this?
So far the rest seems to working, but I need some advice on how to continue. I'd like to make a function that asks which students results the user would like to print and print the students grades and score in each subject. Any tips on how to do this efficiently would be most appreciated! Thanks :D

Code in third post!


Last edited on
Also Im aware I didnt call the CalculateGrade function in main yet in the posted code, sorry!
Updated the code, this is what main looks like now. But still I dont know why it says my objects cannot be defined. Also need advice on how to proceed :/

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
#include <iostream>
#include <string>
#include "StudentGrades.h"

char CalculateGrade(const int gScore) {
	char grade;
	if (gScore > 95)
		return 'A';
	else if (gScore  > 90)
		return 'B';
	else if (gScore > 80)
		return 'C';
	else if (gScore > 70)
		return 'D';
	else if (gScore > 60)
		return 'E';
	else
		return 'F';
}

int main()
{
std::string  courses[5] = { "Mathematics","Swedish","English","History","Physics" };
int score[5];
char grade[5];

Student one(Pacachuti, 42);
Student two(Orjatar,21);
Student three(Rendem, 19);
Student four(Chakkaron, 25);
Student five(Tukktukk, 26);

for (int count = 0; count < 5; count++) {
		std::cout << "Enter mathematics score: ";
		std::cin >> score[0];
		std::cout << "Enter Swedish score: ";
		std::cin >> score[1];
		std::cout << "Enter English score ";
		std::cin >> score[2];
		std::cout << "Enter History score: ";
		std::cin >> score[3];
		std::cout << "Enter Physics score: ";
		std::cin >> score[4];
	}

    char CalculateGrade(const int gScore) {
        for (int i = 0; i < 5; i++){
	char grade;
	if (gScore > 95)
		score[i] = A;
	else if (gScore  > 90)
		score[i] = B;
	else if (gScore > 80)
		score[i] = C;
	else if (gScore > 70)
		score[i] = D;
	else if (gScore > 60)
		score[i] = E;
	else
		score[i] = F;
}
}

	for (int i = 0; i < 5; i++)
	{
		grade[i] = CalculateGrade(score[i]);
	}
}

HEADER
#ifndef STUDENTGRADES_H_INCLUDED
#define STUDENTGRADES_H_INCLUDED

#include <iostream>
#include <string>

class Student {
public:

// Variables
std::string name;
int age;
int count;

//Array variables
std::string  courses[5] = { "Mathematics","Swedish","English","History","Physics" };
int score[5];
char grade[5];

//constructor
Student();
//overload constructor
Student(std::string, int);

//destructor
~Student();

//Function for calculating grade
char CalculateGrade(const int);




};


#endif // STUDENTGRADES_H_INCLUDED



StudentGrades.cpp
#include <iostream>
#include "StudentGrades.h"


Student::Student() {
}

Student::Student(std::string name, int age){

}

Student::~Student() {
}


}



This constructor for Student Student(std::string, int); says it takes two parameters, the a std::string and an integer.

When you create a Student object like this:
Student one(Pacachuti, 42);
what is Pacachuti? It looks like a variable which isn't defined anywhere. Or if it is intended to be a string, it should be enclosed in quotes " ".


The code for function CalculateGrade() seems to appear twice, once as a separate function, and again dumped inside the body of main() (where it has no right to be).
Last edited on
Ahh right you are! Sloppy mistakes, sorry.
This is the code now ;

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
#include <iostream>
#include <string>
#include "StudentGrades.h"

char CalculateGrade(const int);


int main()
{
std::string  courses[5] = { "Mathematics","Swedish","English","History","Physics" };
int score[5];
char grade[5];

Student one("Pacachuti", 42);
Student two("Orjatar",21);
Student three("Rendem", 19);
Student four("Chakkaron", 25);
Student five("Tukktukk", 26);

for (int count = 0; count < 5; count++) {
		std::cout << "Enter mathematics score: ";
		std::cin >> score[0];
		std::cout << "Enter Swedish score: ";
		std::cin >> score[1];
		std::cout << "Enter English score ";
		std::cin >> score[2];
		std::cout << "Enter History score: ";
		std::cin >> score[3];
		std::cout << "Enter Physics score: ";
		std::cin >> score[4];
	}

    char CalculateGrade(const int gScore) {

	char grade;
	if (gScore > 95)
		return = 'A';
	else if (gScore  > 90)
		return 'B';
	else if (gScore > 80)
		grade 'C';
	else if (gScore > 70)
		return 'D';
	else if (gScore > 60)
		return = 'E';
	else
		return= 'F';
    }

	for (int i = 0; i < 5; i++)
	{
		grade[i] = CalculateGrade(score[i]);
	}
}


Why am I getting the "a function definition is not allowed here... " error on line 33? :/
Also how do I best procede to make a function that asks which students results the user would like to print and then prints the students grades and score in each subject? Thanks so much for your help! Lil' lost here. Trying to figure it out on my own as best I can but very short on time here. Cheers!
Why am I getting the "a function definition is not allowed here... " error on line 33? :/


At line 9 the { marks the beginning of main()
At line 54 is the closing brace } of main()

But - you are attempting to define another function CalculateGrade() from lines 33 to 48 wholly inside of main(). This is not possible. You need to move those lines, the entire function definition, and place it after line 54.

Also how do I best procede to make a function that asks which students results the user would like to print and then prints the students grades and score in each subject?

I may be misunderstanding the requirements here. In the opening post it was stated, "make a class of StudentGrades" which to me implies the class would contain at the very least, an array containing the grade in each subject for a single student. It might also contain the student name etc. Then I'd make an array of StudentGrades objects. Do that rather than your current design which has five completely separate student objects, with no grade information.

At least that's what I think. It might help if you posted the whole of the requirements, or at least the key points, in order to understand what you are being asked to do.
One other comment. There is this array:
 
    std::string  courses[5] = { "Mathematics", "Swedish", "English", "History", "Physics" };


I would expect that to be used inside this loop:
1
2
3
4
5
6
7
8
9
10
11
12
    for (int count = 0; count < 5; count++) {
        std::cout << "Enter mathematics score: ";
        std::cin >> score[0];
        std::cout << "Enter Swedish score: ";
        std::cin >> score[1];
        std::cout << "Enter English score ";
        std::cin >> score[2];
        std::cout << "Enter History score: ";
        std::cin >> score[3];
        std::cout << "Enter Physics score: ";
        std::cin >> score[4];
    }
... and instead of accessing individual elements like this: score[3] you would instead use the for loop variable score[count]
Hey! Thank you for your reply!
I changed it up a bit but now I get the error: invalid of non static data member Student::score from this location. And score was not defined in this scope.
What am I doing wrong? Also Im wondering how I should use the std::string courses[5] = { "Mathematics", "Swedish", "English", "History", "Physics" };
within the for loop. Bit lost!

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
#include <iostream>
#include <string>
#include "StudentGrades.h"





int main()
{

Student one("Pacachuti", 42);
Student two("Orjatar",21);
Student three("Rendem", 19);
Student four("Chakkaron", 25);
Student five("Tukktukk", 26);

int i;

for (int i = 0; i < 5; i++) {
		std::cout << "Enter mathematics score: ";
		std::cin >> Student::score[i];
		std::cout << "Enter Swedish score: ";
		std::cin >> Student::score[i];
		std::cout << "Enter English score ";
		std::cin >> Student::score[i];
		std::cout << "Enter History score: ";
		std::cin >> Student::score[i];
		std::cout << "Enter Physics score: ";
		std::cin >> Student::score[i];

	}



	for (int i = 0; i < 5; i++)
	{
		Student::grade[i] = Student::CalculateGrade(score[i]);
	}
}

#ifndef STUDENTGRADES_H_INCLUDED
#define STUDENTGRADES_H_INCLUDED

#include <iostream>
#include <string>

class Student {
public:

// Variables
std::string name;
int age;


//Array variables
std::string  courses[5] = { "Mathematics","Swedish","English","History","Physics" };
int score[5];
char grade[5];

//constructor
Student();
//overload constructor
Student(std::string, int);

//destructor
~Student();

//Function for calculating grade
char CalculateGrade(const int);






};


#endif // STUDENTGRADES_H_INCLUDED

2nd CPP
#include <iostream>
#include "StudentGrades.h"


Student::Student() {
}

Student::Student(std::string name, int age){

}

Student::~Student() {
}

char Student::CalculateGrade(const int gScore) {
	char grade;
	if (gScore > 95)
		return = 'A';
	else if (gScore  > 90)
		return 'B';
	else if (gScore > 80)
		grade 'C';
	else if (gScore > 70)
		return 'D';
	else if (gScore > 60)
		return = 'E';
	else
		return= 'F';
    }








Last edited on
bump! Oh also the criteria are; Create a class of student grades and create 3 arrays (above mentioned arrays). Ask user to input the grade scores for the 5 objects and then print the objects scores and grades in all 5 subjects!

All help is very appreciated ^^
Last edited on
One suggestion, this code:
1
2
3
4
5
6
7
8
9
10
11
12
    for (int count = 0; count < 5; count++) {
        std::cout << "Enter mathematics score: ";
        std::cin >> score[0];
        std::cout << "Enter Swedish score: ";
        std::cin >> score[1];
        std::cout << "Enter English score ";
        std::cin >> score[2];
        std::cout << "Enter History score: ";
        std::cin >> score[3];
        std::cout << "Enter Physics score: ";
        std::cin >> score[4];
    }


should probably look like this:
1
2
3
4
    for (int count = 0; count < 5; count++) {
        std::cout << "Enter " << courses[count] << " score: ";
    	std::cin >> score[count];
    }


I notice there is another thread on the same topic:
http://www.cplusplus.com/forum/beginner/180736/
@ProperConfused

You'd find it much easier to spot and fix these problems, if you used a consistent indentation style. That way, you'd be able to see much more easily which variables are in which scope.

And, for that matter, so would we.

At lines 22 - 30, Student is the name of a class, not an object. score is not a static variable, so you need to identify which object of type Student you're setting the array values for.
Last edited on
Thanks! I understand the problem now, but Im not sure how to fix it efficiently, I know I could manually type in i.e objectX.score[i] for the loops but how can I do it without having to write in every object?
How to make it a part of the loop so it automatically stores the score and grade for each object?
Really new to arrays and dont really have anyone to guide me so really appreciate all the help I can get!
Cheers!
Last edited on
how can I do it without having to write in every object?
I presume you are referring to the Students one, two, three, four and five.
An array of Student objects would seem a reasonable starting point.
If you are using a C+++11 compiler, something like this could work.
Instead of
1
2
3
4
5
    Student one("Pacachuti", 42);
    Student two("Orjatar",21);
    Student three("Rendem", 19);
    Student four("Chakkaron", 25);
    Student five("Tukktukk", 26);
use an array
1
2
3
4
5
6
7
8
    Student students[5] =
    {
         {"Pacachuti", 42},
         {"Orjatar",21},
         {"Rendem", 19},
         {"Chakkaron", 25},
         {"Tukktukk", 26}  
    };

Last edited on
I was down the same line but didn't know how to properly write the array for objects, thank you, most helpful!
How do I actually implement it into the loops though in order for score, grade etc to be declared?

This is my code presently, still a few things clearly off. :/

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
#include <iostream>
#include <string>
#include "StudentGrades.h"





int main()
{


Student students[5] =
    {
         {"Pacachuti"},
         {"Orjatar"},
         {"Rendem"},
         {"Chakkaron"},
         {"Tukktukk"}
    };





    for (int count = 0; count < 5; count++) {
        std::cout << "Enter " << courses[count] << " score: ";
    	std::cin >> score[count];
    }

	for (int i = 0; i < 5; i++)
	{
		grade[i] = CalculateGrade(score[i]);
	}

    for (int i = 0; i < 5; i++)
    {
        td::cout << "Name: " << students[i] << "Course: " << courses[i] << "Score: " << score[i] << "Grade: " << grade[i] << endl;
    }
}

Header

#ifndef STUDENTGRADES_H_INCLUDED
#define STUDENTGRADES_H_INCLUDED

#include <iostream>
#include <string>

class Student {
public:

std::string courses[5] = { "Mathematics","Swedish","English","History","Physics" };
int score[5];
char grade[5];

//constructor
Student();
//overload constructor
Student(std::string);

//destructor
~Student();
};
//Function for calculating grade
char CalculateGrade(const int);







#endif // STUDENTGRADES_H_INCLUDED



2nd cpp

#include <iostream>
#include "StudentGrades.h"


Student::Student() {
}

Student::Student(std::string name){

}

Student::~Student() {
}




char CalculateGrade(const int gScore) {
	char grade;
	if (gScore > 95)
		return'A';
	else if (gScore  > 90)
		return 'B';
	else if (gScore > 80)
		return 'C';
	else if (gScore > 70)
		return 'D';
	else if (gScore > 60)
		return  'E';
	else
		return 'F';
    }







Last edited on
A couple of things I'd like to understand. I see you put the array
std::string courses[5] = { "Mathematics","Swedish","English","History","Physics" };
inside the Student class.

Now I know that in the real world, each student may be studying different subjects, so that might make some sense. But for this particular question, storing the same array many times doesn't necessarily seem like quite the right approach. I'm not clear on what the original question had to say on this. Do all the students study the same five subjects?

One other comment. There are at least two completely different things with the value 5 here.

There is the number of courses. There is the number of students. There is no reason why there should be exactly five students each studying five subjects. Again, I'm not sure what the original question has to say on this.

But when I look at the code and see something like
for (int i = 0; i < 5; i++)
there's no way to tell whether the 5 is the number of students or the number of courses. It means even if the code is correct, it might be correct for the wrong reason.

A usual way to avoid this sort of ambiguity is to avoid having numbers such as 5 scattered throughout the program. Instead, we would use a defined constant, like this:
1
2
const int numCourses = 3;
const int numStudents = 20;

I deliberately made them different just to illustrate that there could easily be such a situation.

Then in a loop, you would put something like
 
for (int i = 0; i < numStudents; i++)
or
 
for (int i = 0; i < numCourses; i++)
as appropriate. You would also use these constants in the array declarations such as
Student students[numStudents] and so on.

Hope this helps to clarify things a little. Though I also hope you can put me right if I've misunderstood the question, I keep feeling I don't have enough information to work with.

Last edited on
Thanks again Chervil for your response! I cant recall right now whether I had to use 5 students or if it was just a number i took but I do see your point in it being confusing, thank you for providing the solution to that. : ) My main problem on line 30, 35, 35 and 40 however remains that score, grade etc is not declared.
I want to make the function so it automatically prints every object with their respective grades and scores.
Also theres an error on line 40; 40|error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"Name: ")) << students[i]'|

Would this be what you had in mind btw?
Many thanks for your 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
#include <iostream>
#include <string>
#include "StudentGrades.h"





int main()
{


Student students[numStudents] =
    {
         {"Pacachuti"},
         {"Orjatar"},
         {"Rendem"},
         {"Chakkaron"},
         {"Tukktukk"}
    };

const int numCourses = 5;
const int numStudents = 5;




    for (int count = 0; count < numStudents; count++) {
        std::cout << "Enter " << courses[count] << " score: ";
    	std::cin >> score[count];
    }

	for (int i = 0; i < numStudents; i++)
	{
		grade[i] = CalculateGrade(score[i]);
	}

    for (int i = 0; i < numStudents; i++)
    {
        std::cout << "Name: " << students[i] << "Course: " << courses[i] << "Score: " << score[i] << "Grade: " << grade[i] << endl;
    }
}



Ok. I'm still not clear on whether all the students are studying exactly the same five courses - in which case the courses array definitely should be removed from inside the student class. Or if they all might study different subjects it could reasonably belong within the student class.

This is a key point, how the rest of the code should look will depend on that decision.

I was just looking through the most recent code, the student name and age variables seem to have disappeared.

I just got as far as trying to code this part:
1
2
3
    for (int count = 0; count < numStudents; count++) 
    {
        std::cout << "Enter Scores for Student: " << students[count].????


I was going to put either name or getName() or something like that but there was nothing there.
After that line there would be a second loop, nested within the first, in order to prompt for the individual scores for that student.

Seriously I'm trying to help but without a clear view of the full requirements, and code which is shifting in unexpected directions it isn't easy.



Im sorry for the lacking information, my apologies. They do all study the same courses in this case. I did remove the age factor also just for simplicity since it was irrelevant to the task!
bump!
My main problem on line 30, 35, 35 and 40 however remains that score, grade etc is not declared.
It was declared in your previous post. Why did you remove the declaration/definition?

Also theres an error on line 40; 40|error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"Name: ")) << students[i]'|
Either you need to overload the 'operator<<' or you need to write down the members individually.
Pages: 12