Checking if variable is a number or a letter: Error

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 22
Reputation: Pokenerd is an unknown quantity at this point 
Solved Threads: 1
Pokenerd Pokenerd is offline Offline
Newbie Poster

Checking if variable is a number or a letter: Error

 
0
  #1
Jul 6th, 2009
Hi, I'm looking for some help with this short program I am writing...

I'm trying to make the sure that the user enters a number when it asks for the temperature in Celsius, and when the user enters a number it works. However if a letter is entered it all goes to hell with the message scrolling down the screen very quickly! Please advise!

If you can think of an alternative way of going around this, and stopping the error from occurring in the first place that would be great!

  1. #include <stdio.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. int main(int nNumberofArgs, char * pszArgs[])
  6. {
  7. for(;;)
  8. {
  9. // enter the temperature in celsius
  10. int nCelsius;
  11. int nIsANumber;
  12.  
  13. nIsANumber = 0;
  14. cout << "Enter the temperature in Celsius: ";
  15. while(nIsANumber == 0)
  16. {
  17. cin >> nCelsius;
  18. if(!isdigit(nCelsius))
  19. {
  20. nIsANumber = 1;
  21. }
  22. else {
  23. cout << "That was NOT a number!!! \nPlease enter the temperature in Celsius: ";
  24. }
  25. }
  26.  
  27.  
  28. //Conversion factor for celsius to fahrenheit
  29. int nFactor;
  30. nFactor = 212 - 32;
  31. int nFahrenheit;
  32. nFahrenheit = nFactor * nCelsius/100 + 32;
  33. //output the results
  34. cout << "Fahrenheit temperature is:";
  35. cout << nFahrenheit;
  36. //loop?
  37. cout << "\n\nWould you like to preform another conversion? \nEnter Y for yes or N for no: ";
  38.  
  39. char cDoContinue;
  40. cin >> cDoContinue;
  41. cout << "\n\n\n";
  42. if(tolower(cDoContinue) == 'n'){
  43. break;
  44. }
  45. else if(!(tolower(cDoContinue) == 'y')){
  46. cout << "You are an idiot... That was not Y or N!!!\n\n";
  47. }
  48.  
  49. }
  50. return 0;
  51. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Checking if variable is a number or a letter: Error

 
0
  #2
Jul 6th, 2009
The atoi function returns an integer from a string. So:
  1. char buf[256];
  2. // Ask for input here
  3. cin.getline(buf,256);
  4. int num = atoi(buf);
  5. if(num == 0)
  6. cout << "Error";
Last edited by u8sand; Jul 6th, 2009 at 8:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Checking if variable is a number or a letter: Error

 
0
  #3
Jul 6th, 2009
Lines 10 and 17. You define nCelsius as an integer, then you read it in as integer. If it's NOT an integer, the >> operation fails. Since you can't ASSUME it's an integer (that's the whole reason you're checking), you can't read it in as an integer. Read it in as a string, THEN check to see whether it's an integer.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Checking if variable is a number or a letter: Error

 
0
  #4
Jul 6th, 2009
Originally Posted by u8sand View Post
The atoi function returns an integer from a string. So:
  1. char buf[256];
  2. // Ask for input here
  3. cin.getline(buf,256);
  4. int num = atoi(buf);
  5. if(num == 0) // atoi returns 0 if it is invalid
  6. cout << "Error";
Which is why you retrieve the input with a string (because a string can handle any text input) and turn it into an integer if its valid.
Last edited by u8sand; Jul 6th, 2009 at 8:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 22
Reputation: Pokenerd is an unknown quantity at this point 
Solved Threads: 1
Pokenerd Pokenerd is offline Offline
Newbie Poster

Re: Checking if variable is a number or a letter: Error

 
0
  #5
Jul 6th, 2009
Originally Posted by VernonDozier View Post
Lines 10 and 17. You define nCelsius as an integer, then you read it in as integer. If it's NOT an integer, the >> operation fails. Since you can't ASSUME it's an integer (that's the whole reason you're checking), you can't read it in as an integer. Read it in as a string, THEN check to see whether it's an integer.

How?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Checking if variable is a number or a letter: Error

 
0
  #6
Jul 6th, 2009
You're trying to test a numerical value for an ASCII character!

if(!isdigit(nCelsius))

isdigit( char ) thus "9834" first character '9'
But its encoded as 9834 an integer.

What they said! Simultaneous posting!
Last edited by wildgoose; Jul 6th, 2009 at 8:44 pm. Reason: note
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 22
Reputation: Pokenerd is an unknown quantity at this point 
Solved Threads: 1
Pokenerd Pokenerd is offline Offline
Newbie Poster

Re: Checking if variable is a number or a letter: Error

 
0
  #7
Jul 6th, 2009
Thanks for your help - I got it working!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC