Why doesn't my program return 0 if the output overflows?

My program takes an user input integer and outputs the reverse. However I need to produce 0 if the reverse overflows a signed integer. I've tried manually with an if else statement to see if the reverse number is greater than or less than the limit but that doesn't work. How can I fix my if statement? Or is there another way of doing this that I'm missing?

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
  #include <iostream>
using namespace std;

int main() {
    int n, reverse = 0;
    
    cout << "Enter an integer: ";
    cin >> n;
    while(n != 0) {
        int remainder = n%10;
        reverse = reverse*10 + remainder;
        n/=10;
    }
    if(reverse<2147483647||reverse>-2147483648)
     {
        cout << "Reversed number = " << reverse << endl;
     }   
     else
     {
        cout << 0 << endl;
     }
    
    
    return 0;
}
Check if the new value of reverse is less than the old value.
How do I check the new value of reverse against the old value?
if(reverse<2147483647||reverse>-2147483648)
This statement is always true, for any number.
How do I check the new value of reverse against the old value?
1
2
3
4
5
6
7
8
9
    while(n != 0) {
        int oldVal = reverse;
        int remainder = n%10;
        reverse = reverse*10 + remainder;
        if (reverse == oldVal) {
            cout << "overflow\n";
        }
        n/=10;
    }
Topic archived. No new replies allowed.