我似乎无法弄清楚我正在尝试构建的这个GPA计算器程序是怎么回事。问题出在printTranscript
类的Student
方法中。该方法调用同一类的getGPA
方法,该方法返回一个double,输出是一些大数而不是标准GPA。
main.cpp中:
#include <iostream>
#include "Student.cpp"
using namespace std;
int main(){
Student stud("Lebron", 23232);
stud.addCourse("Passing", 3, 'A');
stud.addCourse("Finals Record", 4, 'D');
stud.printTranscript();
return 0;
}
Student.h:
#include "Course.h"
#include <vector>
class Student{
private:
string name;
int studentID;
vector<Course> courses;
public:
Student(){
name = "No Name";
studentID = 0;
}
Student(string n, int ID){
name = n;
studentID = ID;
}
string getName(){
return name;
}
void setName(string n){
name = n;
}
int getID(){
return studentID;
}
void setID(int ID){
studentID = ID;
}
void addCourse(string, int, char);
void addCourse(Course);
double getGPA();
void printTranscript();
};
Student.cpp:
#include "Student.h"
void Student::addCourse(string name, int credits, char grade){
courses.push_back(Course(name,credits,grade));
}
void Student::addCourse(Course c){
courses.push_back(c);
}
double Student::getGPA(){
double gradePoints, totalCredits;
for(int i = 0; i < courses.size(); i++){
if(courses[i].getGrade() == 'A'){
gradePoints += (4.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if(courses[i].getGrade() == 'B'){
gradePoints += (3.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if(courses[i].getGrade() == 'C'){
gradePoints += (2.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
else if (courses[i].getGrade() == 'D'){
gradePoints += (1.0 * courses[i].getCredits());
totalCredits += courses[i].getCredits();
}
}
return (gradePoints / totalCredits);
}
void Student::printTranscript(){
cout << "Transcript for: " << name << endl;
cout << "============================" << endl;
for(int i = 0; i < courses.size(); i++){
cout << "Course: " << courses[i].getID() << endl;
cout << "Grade: " << courses[i].getGrade() << endl;
cout << endl;
}
cout << "Overall GPA: " << getGPA() << endl;
}
Course.h:
#include <iostream>
#include <string>
using namespace std;
class Course{
private:
string courseID; // Course name
int numCredits; // Instructor
char letterGrade; // Textbook
public:
Course(string ID, int credits, char grade){ // Assign the course name.
courseID = ID;
numCredits = credits;
letterGrade = grade;
}
string getID(){
return courseID;
}
void setID(string ID){
courseID = ID;
}
int getCredits(){
return numCredits;
}
void setCredits(int credits){
numCredits = credits;
}
char getGrade(){
return letterGrade;
}
void setGrade(char grade){
letterGrade = grade;
}
};
很抱歉代码很长,但我似乎无法弄清楚为什么main中的输出会给我:
Transcript for: Lebron James
============================
Course: Passing
Grade: A
Course: Finals Record
Grade: D
Overall GPA: 2.2321e+230
如果我将主类中的stud.getGPA作为单独的语句cout,它可以正常工作。什么解释了当从同一类的另一个方法调用时输出的大量数字,这是如何解决的?
对于长代码感到抱歉,但我不想错过任何内容,因为我还处于C ++的开始阶段。
答案 0 :(得分:2)
来自dcl.init/7:
默认初始化T类型的对象意味着:
如果T是(可能是cv限定的)类类型,则考虑构造函数。列举了适用的构造函数 ([over.match.ctor]),并选择了最好的初始化程序() 通过重载决议。这样选择的构造函数被调用, 使用空参数列表来初始化对象。
如果T是数组类型,则每个元素都是默认初始化的。
否则,不会执行初始化。
您正在直接进行一些操作+=
:
totalCredits += courses[i].getCredits();
但忘记初始化变量。不要假设最初都将zero
设置为totalCredits
。
因此,将0.0
初始化为-Wuninitialized
。
添加-Wuninitialized
:
根据@ 1201ProgramAlarm的建议,使用warning: 'totalCredits' may be used uninitialized in this function [-Wuninitialized]
标记未初始化的变量。
-Werror
如果您想将警告视为错误,只需添加标记sayName