problem in using isnan()

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

problem in using isnan()

 
0
  #1
May 11th, 2007
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...

  1.  
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. double temp2;
  8.  
  9. int main(){
  10.  
  11. cout << "\n enter something";
  12. cin >> temp2;
  13.  
  14.  
  15. if(isnan(temp2)){
  16. cout << "not a number \n";
  17. }else{
  18. cout << "a number \n";
  19. }
  20. return 0;
  21.  
  22. }

did i used it correctly or not?? help
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: problem in using isnan()

 
0
  #2
May 11th, 2007
Originally Posted by jaepi View Post
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.

  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. double temp2 = 54321 ;
  7.  
  8. int main() {
  9.  
  10. cout << "\n enter something";
  11. cin >> temp2;
  12.  
  13.  
  14. if(isnan(temp2))
  15. cout << "not a number " << temp2 << endl ;
  16. else
  17. cout << "a number " << temp2 << endl ;
  18.  
  19. return 0;
  20. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: problem in using isnan()

 
0
  #3
May 11th, 2007
thank you for that kind and detailed explanation...
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: problem in using isnan()

 
0
  #4
May 11th, 2007
so, if you will check if the input is a number or not, what is the appropriate function to use???
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: problem in using isnan()

 
0
  #5
May 11th, 2007
Originally Posted by jaepi View Post
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.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem in using isnan()

 
0
  #6
May 11th, 2007
>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:
  1. if( !( cin>> temp2 ) ) {
  2. cout << "not a number \n";
  3. } else {
  4. cout << "a number \n";
  5. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: problem in using isnan()

 
0
  #7
May 12th, 2007
Originally Posted by thekashyap View Post
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 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: problem in using isnan()

 
0
  #8
May 14th, 2007
Originally Posted by WaltP View Post
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."
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: problem in using isnan()

 
0
  #9
May 14th, 2007
<offtopic-rant>
I would change the world but God didn't publish the source code..
Smart people can always reverse engineer it... ;-)
</offtopic-rant>
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: problem in using isnan()

 
0
  #10
May 14th, 2007
Originally Posted by ~s.o.s~ View Post
<offtopic-rant>

Smart people can always reverse engineer it... ;-)
</offtopic-rant>
<offtopic-rant>
Then MS Windows would've been stable by now..
</offtopic-rant>
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC