| | |
problem in using isnan()
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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...
did i used it correctly or not?? help
C++ Syntax (Toggle Plain Text)
#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
Retreat!!!
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.
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.

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.
C++ Syntax (Toggle Plain Text)
#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???
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.
Are you Agile.. ?
>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:
You're using formatted input, so you just need to check the error state of the stream object:
C++ Syntax (Toggle Plain Text)
if( !( cin>> temp2 ) ) { cout << "not a number \n"; } else { cout << "a number \n"; }
I'm here to prove you wrong.
•
•
•
•
You can use cin, scanf, gets, getline and so on for reading a string from an input.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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.
"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."
Are you Agile.. ?
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Collapsible table sorting problem (JavaScript / DHTML / AJAX)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: C++ Server: Multi-thread VS Single-thread
- Next Thread: Matrix
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






