Trouble with Class!

I have just learned about Class, and I did one exercise from a book. I do not know why when I insert an invalid input, the program doesn't exit. Please explain!

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
  #include <iostream>
#include <cstdlib>

using namespace std;
class StudentRecord {
public:
    StudentRecord (double firstQuizze, double secondQuizze, double midtermGrade, double finalGrade);
    StudentRecord (double firstQuizze, double secondQuizze, double midtermGrade);
    StudentRecord (double firstQuizze, double secondQuizze);
    StudentRecord (double firstQuizze);
    StudentRecord ();
    void set (double firstQuizze, double secondQuizze, double midtermGrade, double finalGrade);
    double getQuizze1 (); // return first quizze;
    double getQuizze2 ();
    double getMidterm ();
    double getFinal ();
    double getTotalAverageScores ();
    char getLetterGrade ();
    void output (ostream& outs); // output to a file or screen
private:
    double quizze1; // from 0 to 10 {(25/2)% of the grade)
    double quizze2;
    double midterm; // from 0 to 100 (25% of the grade)
    double final; // from 0 to 100 (50% of the grade)
    double totalAverageScore; // total score of all the quizzes, medterm, and final (from 0 to 100)
    char letterGrade;
    void calculateScore ();
    void givingLetterGrade ();
};
int main() {
    
    StudentRecord student1(5, 10, 50, 100);
    cout << "The score of the student 1 is: " << endl;
    student1.output(cout);
    StudentRecord student2(-1, 10, 100, 100); // THIS IS AN INVALID INPUT
    cout << "The score of the student 2 is: " << endl;
    student2.output(cout);
    return 0;
}
StudentRecord::StudentRecord (double firstQuizze, double secondQuizze, double midtermGrade, double finalGrade) {
    if (((firstQuizze > 10) && (firstQuizze < 0)) || ((secondQuizze > 10) && (secondQuizze < 0)) || ((midtermGrade > 100) && (midtermGrade < 0)) || ((finalGrade > 100) && (finalGrade < 0))) {
        cout << "One of the scores is not correct!\n";
        exit(1);
    }
    quizze1 = firstQuizze;
    quizze2 = secondQuizze;
    midterm = midtermGrade;
    final = finalGrade;
}
StudentRecord::StudentRecord (double firstQuizze, double secondQuizze, double midtermGrade) : final(0.0){
//The program seems not go into the if statemts.
    if (((firstQuizze > 10) && (firstQuizze < 0)) || ((secondQuizze > 10) && (secondQuizze < 0)) || ((midtermGrade > 100) && (midtermGrade < 0))) {
        cout << "One of the scores is not correct!\n";
        exit(1);
    }
    quizze1 = firstQuizze;
    quizze2 = secondQuizze;
    midterm = midtermGrade;
}
StudentRecord::StudentRecord (double firstQuizze, double secondQuizze) : final(0.0), midterm(0.0) {
    if (((firstQuizze > 10) && (firstQuizze < 0)) || ((secondQuizze > 10) && (secondQuizze < 0))) {
        cout << "One of the scores is not correct!\n";
        exit(1);
    }
    quizze1 = firstQuizze;
    quizze2 = secondQuizze;
}
StudentRecord::StudentRecord (double firstQuizze) : final(0.0), midterm(0.0), quizze2(0.0) {
    if (((firstQuizze > 10) && (firstQuizze < 0))) {
        cout << "One of the scores is not correct!\n";
        exit(1);
    }
    quizze1 = firstQuizze;
}
StudentRecord::StudentRecord () : final(0.0), midterm(0.0), quizze2(0.0), quizze1(0.0) {
}
void StudentRecord::set(double firstQuizze, double secondQuizze, double midtermGrade, double finalGrade) {
    using namespace std;
    if (((firstQuizze > 10) && (firstQuizze < 0)) || ((secondQuizze > 10) && (secondQuizze < 0)) || ((midtermGrade > 100) && (midtermGrade < 0)) || ((finalGrade > 100) && (finalGrade < 0))) {
        cout << "One of the scores is not correct!\n";
        exit(1);
    }
    quizze1 = firstQuizze;
    quizze2 = secondQuizze;
    midterm = midtermGrade;
    final = finalGrade;
}
double StudentRecord::getQuizze1() {
    return quizze1;
}
double StudentRecord::getQuizze2() {
    return quizze2;
}
double StudentRecord::getMidterm() {
    return midterm;
}
double StudentRecord::getFinal() {
    return final;
}
void StudentRecord::calculateScore() {
    totalAverageScore = (final * (50.0 / 100)) + (midterm * (25.0 / 100)) + ((((quizze1 + quizze2) / 2.0) * 10.0) * (25.0 / 100));
}
double StudentRecord::getTotalAverageScores() {
    calculateScore();
    return totalAverageScore;
}
void StudentRecord::givingLetterGrade () {
    if (totalAverageScore >= 90)
        letterGrade = 'A';
    else if ((totalAverageScore < 90) && (totalAverageScore >= 80))
        letterGrade = 'B';
    else if ((totalAverageScore < 80) && (totalAverageScore >= 70))
        letterGrade = 'C';
    else if ((totalAverageScore < 70) && (totalAverageScore >= 60))
        letterGrade = 'D';
    else
        letterGrade = 'F';
}
char StudentRecord::getLetterGrade() {
    givingLetterGrade();
    return letterGrade;
}
void StudentRecord::output(ostream& outs) {
    outs.setf(ios::fixed);
    outs.setf(ios::showpoint);
    outs.precision(2);
    
    outs << "The first quizze is: " << getQuizze1() << endl;
    outs << "The second quizze is: " << getQuizze2() << endl;
    outs << "The midterm is: " << getMidterm() << endl;
    outs << "The final is: " << getFinal() << endl;
    outs << "The total average score is: " << getTotalAverageScores() << endl;
    outs << endl;
    outs << "The final letter grade is: " << getLetterGrade()<< endl;
    outs << endl;
}
/*
 Sample Dialouge:
 The score of the student 1 is:
 The first quizze is: 5.00
 The second quizze is: 10.00
 The midterm is: 50.00
 The final is: 100.00
 The total average score is: 81.25
 
 The final letter grade is: B
 
 The score of the student 2 is:
 The first quizze is: -1.00
 The second quizze is: 10.00
 The midterm is: 100.00
 The final is: 100.00
 The total average score is: 86.25
 
 The final letter grade is: B
 
 Program ended with exit code: 0
 */
It is a program logic error. The error lies in this line:
if (((firstQuizze > 10) && (firstQuizze < 0)) || ((secondQuizze > 10) && (secondQuizze < 0)) || ((midtermGrade > 100) && (midtermGrade < 0)) || ((finalGrade > 100) && (finalGrade < 0))) {(line 41)
Let's break it down a little bit. Because you used ||, if any of these are correct, there is one input wrong:
1
2
3
4
(firstQuizze > 10) && (firstQuizze < 0)
(secondQuizze > 10) && (secondQuizze < 0)
(midtermGrade > 100) && (midtermGrade < 0)
(finalGrade > 100) && (finalGrade < 0)

But how can firstQuizze, or any value, be more than 10/100 and less than 0 at the same time?
We can fix this by changing them to these:
1
2
3
4
(firstQuizze > 10) || (firstQuizze < 0)
(secondQuizze > 10) || (secondQuizze < 0)
(midtermGrade > 100) || (midtermGrade < 0)
(finalGrade > 100) || (finalGrade < 0)

Now that if one of these values are less than 0 or more than 10/100, like -1 you passed in, it will exit with code 1.
Thanks @Tyler T!
I did not pay close attention on that.
Topic archived. No new replies allowed.