我以前运行过这个程序,它运行良好。然后我将“if”语句添加到“set”方法中,当我运行程序时我开始看到非常大的数字。我能做些什么来解决这个问题,或者有人能告诉我为什么会这样吗?
class GradeBook{
public:
void setStudentID(int ID){
if(10000 <= studentID && studentID <= 50000){
studentID = ID;
}
}
int getStudentID(){
return studentID;
}
void setStudentGrade(int grade){
if(0 <= studentGrade && studentGrade <= 100){
studentGrade = grade;
}
}
int getStudentGrade(){
return studentGrade;
}
void displayMessage(){
cout << "Student " << getStudentID() << " has a score of " << getStudentGrade() << endl;
}
private:
int studentGrade;
int studentID;
};
int main(){
int nameOfID;
int nameOfGrade;
GradeBook gb;
cout << "Please enter a student ID: " << endl;
cin >> nameOfID;
gb.setStudentID(nameOfID);
cout << "Please enter the student's grade: " << endl;
cin >> nameOfGrade;
gb.setStudentGrade(nameOfGrade);
getchar();
gb.displayMessage();
getchar();
}