Grade array help needed!

Pages: 12
Not quite sure what you mean. :/

This is what I want it to do;
Create a studentgrades class, including the three arrays (std::string courses[5] = { "Mathematics","Swedish","English","History","Physics" };
int score[5];
char grade[5];)

Create 5 objects -> Have user input their scores for the 5 subjects they all study -> Convert the score to letter grades -> Print Every object with their scores and grades for each subject so that its clear whose score and grade belongs to whom and to which subject!

I hope that clears everything up. Thanks for your patience, I know Im really lost on this one :-)
Last edited on
What I mean is that you you're trying to access the arrays (lines 29/30/35) as if they were defined in main, but the actually aren't.

I think you are confused about the 5. You have 5 courses and 5 studends, but they are not related. As far as I understand it: Each studends has 5 courses.

How about this:
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
std::string  courses[5] = { "Mathematics","Swedish","English","History","Physics" };

struct course
{
std::string  name;
int score;
char grade;
};

class Student {
public:

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


//Array variables
course data[5];

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

//destructor
~Student();

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


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

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

}

void Output()
{
    cout << "Name: " << name << " ";
    for (int i = 0; i < 5; i++)
    {
        cout << "Course: " << data[i].name << "Score: " << data[i].score << "Grade: " << data[i].grade << endl;
    }
}


};
Ahh thank you so much! That seems to make a lot of sense. Have to run off for a bit but will try it out asap.

Again, many thanks! :)
Hey again!

I'm getting an error with this part of the code on line 26 (: undefined reference to student::CalculateGrade(int)ยด.

(2nd cpp file declarations)

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
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';
    }

void Student::Input() // Note
{
    for (int count = 0; count < 5; count++) {
        std::cout << "Enter " << courses[count] << " score: ";
    	std::cin >> data[count].score;
    }

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


Header file
1
2
3

char CalculateGrade(const int);


What am I messing up here? Many thanks for your time! : )
Last edited on
If CalculateGrade(...) is member of Student you need to add the scope:

char Student::CalculateGrade(const int gScore) {
Haha oh sorry, silly mistake. Get a bit blind after a while. :P
There's no bugs in the code now but I can't get it to work properly, am I calling the functions incorrectly? Or did I mess something else up?
When I run it the program is just blank and then exits "successfully".

Here's the complete code;

Main.cpp
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
#include <iostream>
#include "StudentGradesNew.h"
#include <string>

int main()

{

std::string name;
int age;

Student one("Pacachuti", 22);
Student two("Tukktukk", 23);
Student three("Orjatar", 21);
Student four("Yolo", 27);
Student five("Swaggins", 26);


void Input();




void Output();


return 0;

}






HEADER

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
#ifndef STUDENTGRADES_H_INCLUDED
#define STUDENTGRADES_H_INCLUDED

#include <iostream>
#include <string>



struct course
{
std::string name;
int score;
char grade;
};




class Student {
public:

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


//Array variables
course data[5];


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

//destructor
~Student();

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

void Input();

void Output();
std::string getName();


};

#endif // STUDENTGRADES_H_INCLUDED




2nd CPP

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

Student::Student() {
}

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

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

Student::~Student() {
}

course data[5];

std::string Student::getName() {
return name;
}


char Student::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';
    }

void Student::Input() // Note
{
    for (int count = 0; count < 5; count++) {
        std::cout << "Enter " << courses[count] << " score: ";
    	std::cin >> data[count].score;
    }

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


void Student::Output()
{
    std::cout << "Name: " << name << " ";
    for (int i = 0; i < 5; i++)
    {
        std::cout << "Course: " << data[i].name << "Score: " << data[i].score << "Grade: " << data[i].grade << std::endl;
    }
}





Again, thanks for your help! :)
When I run it the program is just blank and then exits "successfully"

In main(), void Input(); and void Output(); are simply declarations. It tells the compiler that those functions exist - somewhere. It doesn't matter whether they do really exist or not, since the functions are never actually called anywhere.

In fact what I think you want to do is to call the member functions of the Student class. You'd do that by calling the function on an existing Student object, for example:
 
    one.Input();
Thanks again! I put it like you said but for some reason I got the "error: expected initializer before '.' token" on all the rows with the student object.input and .output.



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

int main()

{

std::string name;
int age;

Student one("Pacachuti", 22);
Student two("Tukktukk", 23);
Student three("Orjatar", 21);
Student four("Yolo", 27);
Student five("Swaggins", 26);




void one.Input();
void two.Input();
void three.Input();
void four.Input();
void five.Input();



void one.Output();
void two.Output();
void three.Output();
void four.Output();
void five.Output();

return 0;

}
Last edited on
void one.Input();

This is meaningless - and invalid C++. Why are you putting that "void" in there? You're not declaring or declining the return value of the method (you've already done that in your class definition), so it's meaningless to have that there.

If this is confusing you, you might need to go back to your textbook and familiarize yourself with how to call functions. It's an absolutely fundamental part of C++ (and C, for that matter), so you really should make sure you understand it.
Ah ups, well I sure do need to study a lot more and I am, just started programming with separate files so still figuring things out. Thanks for the pointer!
Program is working quite fine now! Will try to evolve it from here on my own as a project. :-)
Thanks for all your help, especially thanks to you Chervil! Lifesaver. :)

Cheers and have a jolly weekend!
Topic archived. No new replies allowed.
Pages: 12