0

Why if I input any letter does my code then say that it belongs in the interval -1, 1 instead of moving to the else clause and printing the error message?

#include <iostream>

using namespace std;

int main()
{
    double x ;                     
    cout << "Enter a real number : " ;  
    cin >> x ;                     
    

    if ((x >= -1) && (x < 1)) {
        cout << "The nunber you have entered is valid and lies in the interval -1,1!" << endl;
    }
    else if ((x < -1) || (x >= 1)) {
        cout << "Unfortunetely the number you entered is valid but does not lie in the interval" << endl;
    }
    else {
        cout << "Error you have not entered a valid real number!" << endl;
    }
    return 0;
}

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

A double read from user input is always either inside (-1,1] or not. There is no third option. When the user enters something that cannot be parsed as a double then 0 is assigned to x. If you want to check for failure on input you should check the state of the stream. You can do that for example via its conversion to bool inside an if:

#include <iostream>

using namespace std;

int main()
{
    double x ;                     
    cout << "Enter a real number : " ;  
    if (cin >> x) {       // <--- if reading x succeded
        if ((x >= -1) && (x < 1)) {
            cout << "The nunber you have entered is valid and lies in the interval -1,1!" << endl;
        } else if ((x < -1) || (x >= 1)) {
            cout << "Unfortunetely the number you entered is valid but does not lie in the interval" << endl;
        } 
    } else {
        cout << "Error you have not entered a valid real number!" << endl;
    }
    return 0;
}

If you want to read more afterwards you would have to reset the fail state of the stream before.


PS: Floating point numbers are not real numbers, they are all rational.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you so much! Another follow up questions, with floating numbers, I don't understand why decimals like 0.1 and 0.01 have to be approximated which leads to errors in some programs? – Spare_Definition234 Oct 04 '21 at 20:11
  • @Spare_Definition234 numbers are stored in binary. Some reading material: https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf – 463035818_is_not_an_ai Oct 04 '21 at 20:12
  • @Spare_Definition234 Here is a good read: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – NathanOliver Oct 04 '21 at 20:14
  • 1
    This is skirting on false. `A double is always either inside (-1,1] or not. There is no third option.`. This alone is untrue. By definition a NaN is neither inside (-1.1] nor is it not. Its in neither. However, as reading a double in via cin doesn't allow you to read NaNs (for whatever broken reason) then this answer works. But if you did `double x = std::numeric_limits::quiet_NaN();` then this answer fails whereas the OP's original attempt (which has a third option to the if/else clause) works well. – Mike Vine Oct 04 '21 at 21:03
  • @MikeVine you are absolutely right, only "But if you did `double x = ...` then this answer fails " is incorrect since C++11. `std::cin >> x` will either suceed or assign `0` (unless `std::cin` was already in a fail state before, then nothing happens). – 463035818_is_not_an_ai Oct 05 '21 at 07:34