Display message with double?

I need to display an error message when the GPA is outside the bounds of 0.0 < gpa < 4.0 (so like gpa = 5.0) however I am not sure how to do this since my variable 'g' is declared as double and not string. If it were string I understand that in my else statement I could just do something like gpa = "Error Message" But how would I go about displaying an error message with a double? I put an example of what I did for "Name" to get an idea of what I'd like it to do and also what I have for "GPA" without the error included. If someone could help me with this I would truly appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
///////// NAME ///////
void Student::setName(string n) {
    if ( isupper(n[0]) )   // 1st character of name must be A-Z
        name = n;
    else
        name = "----Bad name entered--";
}
string Student::getName() {
    return name;
}

  /////// GPA /////////
void Student::setGpa(double g) {
    if (g>=0.0 && g<=4.0)         // gpa must be from 0.0 to 4.0
        gpa = g;
    else
        gpa = 0;
}
double Student::getGpa() {
    return gpa;
}
When do you need to display an error?
To whom do you need to display an error?

Who should check whether a value is out of range?
Where does that value come from?
Who should handle the error situation?
In my main file I have an array that has a list of 5 students, their name, ID, GPA and major
i.e
1
2
3
Student CLASS54[] = {
Student ("Sally Smith", 55458, 5.0, "MATH"),
};


Then I begin my main program

1
2
3
4
5
6
int main(int argc, char* argv[]) {
    int NumberOfStudents = sizeof(CIS054)/sizeof(Student);

 // List all the students in the course, their ID, GPA, and Major
    for (int i=0; i<NumberOfStudents; i++)
        cout << setw(1) << "  " << CIS054[i].getIdNumber() << "  " << CIS054[i].getName() << setw(5) << " " << CIS054[i].getGpa() << setw(5) << "  " << CIS054[i].getMajor() << endl;


this is where the error will display.
Last edited on
Array as a global variable? Rethink about that. Globals, like dark side of the Force, seem quick and easy but trap you unnecessarily.

You do set the values for each Student with a constructor. Does that warrant separate setName and setGPA? Can the name of a student change later?

Overridden output operator for Student would be a nice addition though. To be able to write:
1
2
3
for ( const auto & student : CIS054 ) {
  std::cout << student << '\n';
}


Or perhaps something like:
1
2
3
4
5
6
7
for ( const auto & student : CIS054 ) {
  if ( student.hasValidID() ) {
    std::cout << student << '\n';
  } else {
    std::cout << "fake student\n";
  }
}

The point is that the Student may have whatever it has, but it can tell (with booleans) about itself and the caller, like main(), can opt to ask questions and act based on the answers.

Alternatively, member functions can write to std::cout as a side-effect. Think what happens, if the constructor writes and the global array is initialized with poor students ...


PS. "1st character of name must be A-Z" does allow a "valid student":
Anne-maRie von kaRajan?
Topic archived. No new replies allowed.