overload constructor and accessor constructor issue

Having a problem with classes and vector interaction. So far I can tell in the void fillVector function that the overload constructor works in the sense that the info makes it there, however when I attempt to output from anywhere besides the overload constructor such as with the accessor functions it seems the data doesn't exist! I don't see why the values for newCredits and newGrade wouldn't be output... if the constructor is defining them.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include "GPA.h"
#include <iomanip>
#include <vector>
#include <string>

void fillVector(std::vector<GPA>&);

void printVector(const std::vector<GPA>&);

void printGPA(std::vector<GPA>&);

int main(int argc, const char * argv[])
{
    std::vector<GPA> myGpa;
    
    fillVector(myGpa);
    //printVector(myGpa);
    //printGPA(myGpa);
    
    return 0;
}

void fillVector(std::vector<GPA>& newClass)
{
    std::string grade;
    int classes;
    int credits;
    std::cout << "How many classes are you taking: ";
    std::cin >> classes;
    for (int i = 0; i < classes; i++)
    {
        std::cout << "Enter class #" << i + 1 << " credit hours: ";
        std::cin >> credits;
        std::cout << "Enter class #" << i + 1 << " grade: ";
        std::cin >> grade;
        GPA newStudent(credits, grade);
        newClass.push_back(newStudent);
        std::cout << "\n";
    }
}

void printVector(const std::vector<GPA>& newClass)
{
    unsigned int size = newClass.size();
    for (unsigned int i = 0; i < size; i++)
    {
        std::cout << "Class #" << i + 1 << " credit hours: " << newClass[i].getCredits() << "\n";
        std::cout << "Class #" << i + 1 << " grade: " << newClass[i].getGrade() << "\n" "\n";
    }
}

void printGPA(std::vector<GPA>& newClass)
{
    int total(0);
    double gpa = 0.0;
    
    unsigned int size = newClass.size();
    for (unsigned int i = 0; i < size; i++)
    {
        int temp = newClass[i].getCredits() * 4;
        total = total + temp;
    }
    std::vector<double> converter;
    size = newClass.size();
    for (unsigned int i = 0; i < size; i++)
    {
        std::string temp = newClass[i].getGrade();
        double grade = newClass[i].setCreditsEarned(temp);
        converter.push_back(grade);
    }
    
    for (unsigned int i = 0; i < size; i++)
    {
        double temp = converter[i] * newClass[i].getCredits();
        gpa = gpa + temp;
    }
    
    std::cout << gpa / total;
 
#include <iostream>
#include <string>
#include <vector>

#ifndef __GPA__GPA__
#define __GPA__GPA__

#include <stdio.h>

class GPA
{
public:
//Constructor
GPA();

//Overload Constructor
GPA(int, std::string);

//Destructor
~GPA();
    
//Accessor Functions
int getCredits() const;
    
std::string getGrade() const;
    
//Mutator Functions
double setCreditsEarned(std::string);

int setTotalCredits(int);
    
double getGPA(double, int);
    
int totalCredits;
    
int creditsEarned;
    
private:
    
int newCredits;
    
std::string newGrade;

};

#endif /* defined(__GPA__GPA__) */

   
}


#include "GPA.h"

GPA::GPA()
{
    int credits = 0;
}

GPA::GPA(int credits, std::string grade)
{
    int newCredits = credits;
    std::string newGrade = grade;
}

//Accessor Functions
int GPA::getCredits() const
{
    return newCredits;
}

std::string GPA::getGrade() const
{
    return newGrade;
}

//Mutator Functions
double GPA::setCreditsEarned(std::string grade)
{
    double converter = 0.0;
    if (grade == "A")
    {
        converter = 4.0;
    }
    else if (grade == "A-")
    {
        converter = 3.66;
    }
    if (grade == "B+")
    {
        converter = 3.33;
    }
    if (grade == "B")
    {
        converter = 3.0;
    }
    if (grade == "B-")
    {
        converter = 2.66;
    }
    if (grade == "C+")
    {
        converter = 2.33;
    }
    if (grade == "C")
    {
        converter = 2.0;
    }
    if (grade == "C-")
    {
        converter = 1.66;
    }
    if (grade == "D+")
    {
        converter = 1.33;
    }
    if (grade == "D")
    {
        converter = 1.0;
    }
    if (grade == "D-")
    {
        converter = .66;
    }
    else
    {
        converter = 0.0;
    }
    return converter;
}

int GPA::setTotalCredits(int credits)
{
    return credits * 4;
}

double GPA::getGPA(double creditsEarned, int totalCredits)
{
    return creditsEarned / totalCredits;
}

GPA::~GPA()
{
    
}





On lines 139-143 you are not assigning to your private variables; you are defining new local variables which are lost.
Thank you so much! I knew it was small but I just wasn't seeing that obvious issue, it was driving me crazy.
Topic archived. No new replies allowed.