Help with NaN function

Hi everyone, I am trying to write a function that can help me differenciate between a number and another character. I read about several ways to do this and I have tried severals but I have no luck. I thought I was going to be able to this code I wrote last night. However, I can't get inside the if statements. Any value returns the else condition "This is a number".

I am using Visual C++ 2008. Any help would be appreciated.


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

#include <iostream> 
#include <math.h> 
#include <limits> 
#include <float.h>
#include <cmath>
 
using namespace std; 

inline bool isnan(double x) 
{ 
   return x != x; 

}
 
int main( ) 
{ 

    double f_nan;
 

    cout << "enter a number: " << endl; 
    cin >> f_nan;
 

    if( isnan(f_nan) ) 
    { 
        cout << "This is not a number" << f_nan << endl; 
    } 

    else 
   {
        cout << "This is a number" << endl;
   }

}


You cannot enter valid floating point number and expect to not be a number. A floating point value is not a number when the bit pattern is a meaningless encoding for the particular type of number.
You are asking about Input Validation

Basic info:
http://www.cplusplus.com/forum/beginner/15655/#msg77321

Something fancy:
http://www.cplusplus.com/forum/beginner/13044/#msg62827

Good luck!
Topic archived. No new replies allowed.