Return error if variable exceeds maximum limit

I've created a class named shape, and it's working perfectly, but I'd like to implement a new feature. When the user enters a value bigger than the data structure long double can take, return an error or when the volume of the shape is larger than the limit of long double return again an error. I don't want the kind of error that just outputs something. I need it to terminate the program, in such way, that the user knows it is because of an error. By the way, this is not an assignment or something, I'm just asking because I'm curious. This is my code:

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
#include<iostream>
using namespace std;
typedef long double ld;
class shape{
private:
    ld length;
    ld width;
    ld height;
public:
    void make_shape(ld a, ld b, ld c){
     length = a;
     width = b;
     height = c;
    }
    ld volume(){
     ld answer = length * width * height;
      return answer;
    }

};

int main(){
int n;
cin>>n;
shape cubes[n];
ld a, b, c;
for (int j=0; j<n; j++){
        cin>>a>>b>>c;
    cubes[j].make_shape(a, b, c);
}
for (int i=0; i<n; i++){
    cout<<cubes[i].volume()<<"\n";
}
}
Last edited on
Topic archived. No new replies allowed.