Constructors/ Vector of objects

Hello there, I am relatively new to programming. I am working on a program for my intro to cs class, but I am having trouble with it.
My program is supposed to calculate your grade for any class.
It asks for the total components (homework, quiz, exams etc.) and calculates the points for each component.
A problem i noticed is that the default constructor doesn't initialize any of the member variables i provided.
Another problem I came across is on the last step. Once i fill the vector with the objects, It gives me bogus values when i want them to be printed.


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
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

class Grade
{
private:
	string coursename;
	double coursePercent;
	int totalassignments;
	double maxpoints;
	double userpoints;
	double userpercent;

public:
	// Constructor
	Grade();

	// Reads in course component name, number of total assignments, and percentage of component and computes maximum component points
	void read();

	// computes percentage of all assignments in category
	double computePercentage(double size);

	// returns totalassignments
	int get_size();

	// prints course name, maxpoints, userpoints
	void print();

	void test();

};
/*------------------------------------------------*/
Grade::Grade()
{
	double coursePercent = 0;
	int totalassignments = 0;
	double maxpoints = 0;
	double userpoints = 0;
	double userpercent = 0;
}

void Grade::read()
{
	cout << "What is the name of the course component " << endl;
	cin >> coursename;
	cout << "What is the percentage of this course component?" << endl;
	cin >> coursePercent;
	cout << "How many assignments in this course component: " << endl;
	cin >> totalassignments;
	maxpoints = totalassignments * 100;										// calculates maximum points in course component
	
}

int Grade::get_size()
{
	return totalassignments;
}

double Grade::computePercentage(double size)
{
	cout << "Enter your scores: " << endl;
	int input;
	vector<double> scores(size);
	for (int i = 0; i < scores.size(); i++)
	{
		cin >> input;
		scores[i] = input;
	}

	for (int i = 0; i < scores.size(); i++)
	{
		userpoints = userpoints + scores[i];
	}

	userpercent = userpoints / maxpoints;
	userpercent = userpercent * coursePercent;

	cout << "Your score for this component: " << userpercent << endl;

	return userpercent;
}

void Grade::print()
{
	cout << setw(15) << coursename << setw(10) << coursePercent << setw(12) << userpercent << endl;
}

void Grade::test()
{
	cout << coursePercent << endl;
	cout << coursename << endl;
	cout << maxpoints << endl;
	cout << userpercent << endl;
	cout << totalassignments << endl;

}


/*--------------------------------------------------------------------*/

int main()
{

	cout << "How many course components do you have?" << endl;
	int comp;
	cin >> comp;

	vector<Grade> YourGrade(comp);

	Grade component;

	//fills vector with course component details
	for (int i = 0; i < comp; i++)
	{
		component.read();
		component.computePercentage(component.get_size());
		YourGrade.push_back(component);
	}

	//prints information inputted
	cout << setw(15) << "Course Component " << setw(10) << "Max " << setw(12) << "Your score " << endl;
	for (int i = 0; i < comp; i++)
	{
		YourGrade[i].print();
	}


	system("pause");
	return 0;
}
In the constructor for Grade, do not declare local variables and initialise them.
Instead, assign to the member variables.
1
2
3
4
5
6
7
8
9
10
Grade::Grade()
{
    // do not declare new local variables.
    // instead assign to the member variables
	/*double*/ coursePercent = 0;
	/*int*/ totalassignments = 0;
	/*double*/ maxpoints = 0;
	/*double*/ userpoints = 0;
	/*double*/ userpercent = 0;
}



In Grade::computePercentage, reset userpoints to zero before you accumulate into it. And change the type of the argument size to an integral type.
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
double Grade::computePercentage(int size)
{
	cout << "Enter your scores: " << endl;
	int input;
	vector<double> scores(size);
	for (int i = 0; i < scores.size(); i++)
	{
		cin >> input;
		scores[i] = input;
	}

        userpoints = 0 ; // *** reset user points to zero first

	for (int i = 0; i < scores.size(); i++)
	{
		userpoints = userpoints + scores[i];
	}

	userpercent = userpoints / maxpoints;
	userpercent = userpercent * coursePercent;

	cout << "Your score for this component: " << userpercent << endl;

	return userpercent;
}



In main(), create an empty vector YourGrade and then push back items into it.
1
2
3
// create an empty vector; we are going to push_back 
// comp number of items into it later
vector<Grade> YourGrade ; /*(comp) */;



Ideally, add const qualifiers to the members get_size and print (both in the declaration and in the definition)
See: https://isocpp.org/wiki/faq/const-correctness#const-member-fns
Last edited on
Hi GISMO07

i've done following changes to your code... it's working on my machine...

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
double Grade::computePercentage(int size)
{
    cout << "Enter your scores: " << endl;

    // create a vector, you don't have to put a fixed size -> because you push
    vector<double> scoreTable;
    
    //set your variables to default 0, if not you could have random numbers inside
    double input = 0;
    double userpoints = 0;

    //go trough inputs like given size in paramenter
    for(int i=0; i<size; i++){
        
        cout << "Enter input: ";
        cin >> input;

        // userpoints = userpoints + input;
        userpoints += input;
        
        // push the input in vector-container, you dont have to resize
        scoreTable.push_back(input);
    }

    //go trough vector with an iterator (pointer) and calc percentage of user
    for(vector<double>::iterator iter = scoreTable.begin(); iter != scoreTable.end(); iter++){
        userPercent = userpoints / maxPoints;
        userPercent *= coursePercent;
    }
    return userPercent;
}


check the private section of class file...

1
2
3
4
5
    string coursename;
    double coursePercent;
    double totalAssignments;
    double maxPoints;
    double userPercent;


constructor looks like...

1
2
3
4
5
6
7
8
Grade::Grade()
{
    coursename = "";
    coursePercent = 0.0;
    totalAssignments = 0.0;
    maxPoints = 0.0;
    userPercent = 0.0;
}


When you count a double with an int, the result ist int (splitted after the dot .)... pay attention to that... you have two possibilities... "cast" the int-variable when you calculate or declare it as double like I did... it's up to you...

then i have done changes in the readIn-function... you have to check if the entered totalAssignment ist not 0... if it's 0 you can't create the maxPoints...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Grade::readIn()
{
    cout << "Please put Course Name: ";
    getline(cin, coursename);

    cout << "Please put Course Percent (double): ";
    cin >> coursePercent;

    cout << "Please put Total Assignment (double): ";
    cin >> totalAssignments;

    if(totalAssignments == 0){
        cout << "cannot calculate points in component, totalAssignemnts is 0!" << endl;
    } else {
        maxPoints = totalAssignments * 100;
    }
}


the main looks like...

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    Grade component;
    component.readIn();
    component.test();

    cout << "Percentage: " << component.computePercentage(3) << endl;

    return 0;
}


with this kind of architecture, you will have problems, when you create a second object... but thats another problem ;) test it, and you will see...

hope it helps you...

regards,
p3p3
Last edited on
I caught the mistake when i made when defining the constructor later. I tried emailing my professor but by the time he responds, the assignment might be overdue. But thanks for the feedback i was able to get the program going. Thank you guys!
Topic archived. No new replies allowed.