-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaiseTut_ExceptionHandle
62 lines (50 loc) · 1.42 KB
/
RaiseTut_ExceptionHandle
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
#include <iostream>
#include <exception>
using namespace std; // Add this line to use std namespace
class InvalidResultException : public exception {
public:
InvalidResultException(const string& message) : message(message) {}
const string& getMessage() const {
return message;
}
private:
string message;
};
class ExamResults {
private:
int passedCount = 0;
int failedCount = 0;
public:
void enterTestResult(int result) {
if (result == 1) {
passedCount++;
} else if (result == 2) {
failedCount++;
} else {
throw InvalidResultException("Invalid input: Result must be 1 (passed) or 2 (failed).");
}
}
void displaySummary() {
cout << "Summary of Exam Results:" << endl;
cout << "Number of students who passed: " << passedCount << endl;
cout << "Number of students who failed: " << failedCount << endl;
if (passedCount > 8) {
cout << "Raise tuition." << endl;
}
}
};
int main() {
ExamResults results;
for (int i = 0; i < 10; i++) {
int result;
cout << "Enter result (1 for passed, 2 for failed): ";
try {
cin >> result;
results.enterTestResult(result);
} catch (InvalidResultException& e) {
cerr << "Error: " << e.getMessage() << endl;
}
}
results.displaySummary();
return 0;
}