-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvg_Exception Handling.cpp
65 lines (55 loc) · 1.64 KB
/
Avg_Exception Handling.cpp
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
//using exception handling
#include <iostream>
class Number {
public:
virtual double getAverage() = 0;
};
class InvalidInputException : public std::exception {
public:
InvalidInputException(const std::string& message) : message(message) {}
const std::string& getMessage() const {
return message;
}
private:
std::string message;
};
class FiveNumberAverage : public Number {
private:
double numbers[5];
public:
FiveNumberAverage(double num1, double num2, double num3, double num4, double num5) {
try {
validateInput(num1, num2, num3, num4, num5);
} catch (InvalidInputException& e) {
throw e;
}
numbers[0] = num1;
numbers[1] = num2;
numbers[2] = num3;
numbers[3] = num4;
numbers[4] = num5;
}
double getAverage() override {
double sum = 0.0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
return sum / 5.0;
}
private:
void validateInput(double num1, double num2, double num3, double num4, double num5) {
if (std::isnan(num1) || std::isnan(num2) || std::isnan(num3) || std::isnan(num4) || std::isnan(num5)) {
throw InvalidInputException("Invalid input: Input cannot be NaN.");
}
}
};
int main() {
try {
FiveNumberAverage number(1.0, 2.0, 3.0, std::numeric_limits<double>::infinity(), 5.0);
double average = number.getAverage();
std::cout << "The average of the numbers is: " << average << std::endl;
} catch (InvalidInputException& e) {
std::cerr << "Error: " << e.getMessage() << std::endl;
}
return 0;
}