943,563 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1610
  • C++ RSS
Jul 6th, 2009
0

Checking if variable is a number or a letter: Error

Expand Post »
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!

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
Pokenerd is offline Offline
37 posts
since Jul 2009
Jul 6th, 2009
0

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

The atoi function returns an integer from a string. So:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 6th, 2009
0

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

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Jul 6th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by u8sand ...
The atoi function returns an integer from a string. So:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 6th, 2009
0

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

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?
Reputation Points: 10
Solved Threads: 2
Light Poster
Pokenerd is offline Offline
37 posts
since Jul 2009
Jul 6th, 2009
0

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

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
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

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

Thanks for your help - I got it working!
Reputation Points: 10
Solved Threads: 2
Light Poster
Pokenerd is offline Offline
37 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Random number generators
Next Thread in C++ Forum Timeline: question about if/else





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC