class and array[]

I know a lot of stuff is probably wrong or not the best way to do it, but I am learning. If someone could point me in the right direction or give me a hint. I am having trouble passing and using the array in the class.

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
#include <iostream>
using namespace std;
class studentsGrades {
private: 
	float average = 0;
	float grade = 0;
	
public:
	void startPrint();
	void averageGrades(int *p, int grade);
	

};
void studentsGrades::startPrint()
{
	cout << "How many grades will you enter?" << endl;
}
void studentsGrades::averageGrades(int *p, int grade)
{
	int gradesEnter = 0;
	cout << "Enter Grade: " << endl;
	for (int i = 0; i < grade; i++)
		{
			cin >> gradesEnter;
			p[grade] = gradesEnter;
			cout << "Enter grade;" << endl;
		
		}
	cout << p[1];
}
int main() {
	
	int grades = 0;
	studentsGrades object;
	object.startPrint();
	cin >> grades;

	int *p;
	
	p = new int[grades];
	/*	if (p == nullptr)
		{
			cout << " Memory can not be allocated..." << endl;
		}*/
	object.averageGrades(p, grades);

	return 0;
What are you having trouble with exactly? You seem to be doings pretty well (Just need to alter your std::cout statement a bit for better input/output in your for loop) and your program runs fine.
Are you talking about line 29? Im just trying to display the result to test each step before i move on. I'm getting a long number, -842150451. is this an address by the way?

edit: okay ignore what I said, I am unsure what you are talking about.lol..
Last edited on
Line 29 looks like it has a number one instead of the letter i.
Well I just wanted to be sure the array was working. So I wanted it to print a value just to test. So i chose the 2nd element.
I'm getting a long number, -842150451. is this an address by the way?

No, it's an uninitialized value. You never assign any values to your array, but you do access the array out of bounds within that loop in averageGrades().

Look closely at the following snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
void studentsGrades::averageGrades(int *p, int grade)
{
	int gradesEnter = 0;
	cout << "Enter Grade: " << endl;
	for (int i = 0; i < grade; i++)
		{
			cin >> gradesEnter;
			p[grade] = gradesEnter;  // You are trying to access the array out of bounds here. 
			cout << "Enter grade;" << endl;

		}
	cout << p[1];
}


What value does grade have at the line noted above?

Also note that that the parameter grade hides the class member variable of the same name.
ha!... stupid me. silly mistake.

yea I got distracted on making this array work and should've renamed one or the other.

Thanks all!
But the real problem is not the name of the parameter or member variable, the problem is that you're using the wrong index value for the index of the array.

Last edited on
yes, a silly mistake.
ok someone let me know if there is anything worth improving.
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
#include <iostream>
using namespace std;
class studentsGrades {
private: 
	float average = 0;
	float sum = 0;
	float gradesEnter = 0;
	int numberOfGrades = 0;
	float *p;
public:
	void startPrint();
	void averageGrades(int numberOfGrades);

};
void studentsGrades::startPrint()
{
	cout << "How many grades will you enter?" << endl;
	cin >> numberOfGrades;
	averageGrades(numberOfGrades);
}


void studentsGrades::averageGrades(int numberOfGrades)
{

	p = new float[numberOfGrades];

	
	for (int i = 0; i < numberOfGrades; i++)
	{
		cout << "Enter grade: " << endl;
		cin >> gradesEnter;
		p[i] = gradesEnter;
		sum += p[i];

	}
	average = sum / numberOfGrades;
	cout << " average of " << numberOfGrades << " 's is : " << average;
}
int main() {
	
	
	studentsGrades object;
	object.startPrint();
	


	

	return 0;



}


edit: forgot to delete the memory
Last edited on
You have all those variables in your class, why aren't you using any of them?

Also this:
1
2
3
4
5
6
7
8
9
	
	for (int i = 0; i < numberOfGrades; i++)
	{
		cout << "Enter grade: " << endl;
		cin >> gradesEnter;
		p[i] = gradesEnter;
		sum += p[i];

	}

Could be simplified to:
1
2
3
4
5
6
7
	
	for (int i = 0; i < numberOfGrades; i++)
	{
		cout << "Enter grade " << i << ": ";
		cin >> p[i];
		sum += p[i];
	}

I would also suggest that averageGrades() only compute the average of the grades. Get the values for the array, do the memory allocation, in another function. You should strive to keep your functions doing as little as possible. The same with your print function, it should only print the values stored in your class, and it really should be const qualified.

Also you should store the value entered for the number of grades into the class, and you should have a destructor, copy constructor, and assignment operator because you have a pointer member variable.

Also now the parameter numberOfGrades is hiding the member variable with the same name.

Are you required to use the array and dynamic memory? If not you should really consider using a std::vector instead of the array.
Last edited on
What do you mean by not using any? I was thinking that's what I did. please explain.

I just figured out five minutes ago how to move the array between functions so I have updated that into a separate function.

I will work on implementing the constructor and destructor now. I have not learned these two things nor the copy constructor. Is a constructor where i initialize the variables? In this case to 0? hmm..

I have not taught myself about vectors. I kind of skipped them somehow. This was just some free code not an assignment.
I just figured out five minutes ago how to move the array between functions so I have updated that into a separate function.

Why are you trying to "move the array between functions"? The array should be a class member function, then you wouldn't need to pass the array between your functions.

IMO your class should only have two member variables, the pointer for the array and a variable for the size of the array. The average could be computed when you need it by calling the average function. Also you really should be using double instead of the float. The float type should only be used when speed is the top priority and the loss of precision is not a concern.

I have not taught myself about vectors.

You should really consider studying up on vectors, they're safer and easier to use than arrays. Plus you would then not need to worry about dynamic memory allocation or the destructor, copy constructor, and assignment operator.

What do you mean by not using any?

You have several variables that are unused or that should be variables local to the function. For example sum should be local to the function, not a member variable. Then you have a class member function that has a parameter with the same name as the class member variable. This means that the class member variable is not being used, your using the parameter, these two variables are different variable (even though the have the same names). I probably overstated the problem because I didn't take a real close look at your second code fragment.




So, allocate the memory in one functions and point to it from the average function?

I will get to vectors tomorrow, I'm done with this shit for tonight..

I can see how some variables should be local to those functions.


Do you think passing parameters from main is what I should be doing? I'm kind of confused on how much I should do in main and how much in the class.
Do you think passing parameters from main is what I should be doing?

What would you be using the parameters for? You could do this whole assignment by calling a series of class member functions with no parameters if you so desire.

I'm kind of confused on how much I should do in main and how much in the class.

There is really no one correct answer for this question, it depends on how the class is designed.

Topic archived. No new replies allowed.