hello there...i've been using this function to know if the input of the user is a number or a letter...but it's not working...

#include <iostream>
#include <math.h>

using namespace std;

double temp2;

int main(){
    
    cout << "\n enter something";
    cin >> temp2;


    if(isnan(temp2)){
        cout << "not a number \n";
    }else{
        cout << "a number \n";
    }
    return 0;

}

did i used it correctly or not?? help

but it's not working...

Something more than "it's not working" is always more helpful :)

A few things I can think of are:
- isnan() should be used when you have doubts abt the integrity of the number (you know it's a number already, but may not be represeted correctly in the memory).
- cin would ensure that if what user entered is not a number it doesn't modify the value of the double variable you supplied.
- so when you enter some junk on the command line, temp2 is never modified and remains with uninitialized junk, but it's still NOT an NaN, it's a junk number.
- OTOH if a correct number is entered all is well.
- So in either case when isnan() is called with temp2, it should say it's NOT an NaN.

Here is a small thing you can do to get more insight:
Run it twice and enter junk value and valid value.

#include <iostream>
#include <math.h>

using namespace std;

double temp2 = 54321 ;

int main() {
    
    cout << "\n enter something";
    cin >> temp2;


    if(isnan(temp2))
        cout << "not a number " << temp2 << endl ;
    else
        cout << "a number " << temp2 << endl ;

    return 0;
}

Lastly if you ask me to guess where would one use isnan(), well I think you might wanna do it when you're reading serialized binary data and decoding it, or reading from socket etc and decoding parts of a msg.

so, if you will check if the input is a number or not, what is the appropriate function to use???

so, if you will check if the input is a number or not, what is the appropriate function to use???

Well, you'll have to change the way you are taking input if you want YOUR code to do this check instead of cin.
Accept a string from the user instead of a number. Once you have the string you can use functions like isalpha() and isXXX() to do the validation.
You can use cin, scanf, gets, getline and so on for reading a string from an input.
Just check out other threads I've seen more than enough of threads regarding methods of taking string inputs, issues/problems related to them and their solutions.
I would strongly recommend that you read up these threads so you know the issues with each method and can choose wisely.

>so, if you will check if the input is a number or not, what is the appropriate function to use???
You're using formatted input, so you just need to check the error state of the stream object:

if( !( cin>> temp2 ) ) {
  cout << "not a number \n";
} else {
  cout << "a number \n";
}

You can use cin, scanf, gets, getline and so on for reading a string from an input.

The only appropriate function listed for reading a string is getline(). All the rest have problems reading strings, from stopping before the full string is read to potentially crashing the program. I'd also add fgets() as a good function.

The only appropriate function listed for reading a string is getline(). All the rest have problems reading strings, from stopping before the full string is read to potentially crashing the program. I'd also add fgets() as a good function.

Yup, that's why I also added (just below the line you quoted) the following:
"Just check out other threads I've seen more than enough of threads regarding methods of taking string inputs, issues/problems related to them and their solutions.
I would strongly recommend that you read up these threads so you know the issues with each method and can choose wisely."

<offtopic-rant>

I would change the world but God didn't publish the source code..

Smart people can always reverse engineer it... ;-)
</offtopic-rant>

<offtopic-rant>

Smart people can always reverse engineer it... ;-)
</offtopic-rant>

<offtopic-rant>
Then MS Windows would've been stable by now.. :)
</offtopic-rant>

This article has been dead for over six months. Start a new discussion instead.