I have run this program before and it worked fine. Then I added the “if” statements to the “set” methods and i started seeing very large numbers when I ran the program. What can I do to fix this problem or can someone enlighten me as to why this is happening?
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();
}
You have your comparisons wrong you meant.
You don’t have else statements to make sure the variable is initialized, thus I’d change it to:
That will hopefully fix your issues.