| | |
Checking if variable is a number or a letter: Error
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 22
Reputation:
Solved Threads: 1
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!
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)
#include <stdio.h> #include <iostream> using namespace std; int main(int nNumberofArgs, char * pszArgs[]) { for(;;) { // enter the temperature in celsius int nCelsius; int nIsANumber; nIsANumber = 0; cout << "Enter the temperature in Celsius: "; while(nIsANumber == 0) { cin >> nCelsius; if(!isdigit(nCelsius)) { nIsANumber = 1; } else { cout << "That was NOT a number!!! \nPlease enter the temperature in Celsius: "; } } //Conversion factor for celsius to fahrenheit int nFactor; nFactor = 212 - 32; int nFahrenheit; nFahrenheit = nFactor * nCelsius/100 + 32; //output the results cout << "Fahrenheit temperature is:"; cout << nFahrenheit; //loop? cout << "\n\nWould you like to preform another conversion? \nEnter Y for yes or N for no: "; char cDoContinue; cin >> cDoContinue; cout << "\n\n\n"; if(tolower(cDoContinue) == 'n'){ break; } else if(!(tolower(cDoContinue) == 'y')){ cout << "You are an idiot... That was not Y or N!!!\n\n"; } } return 0; }
The atoi function returns an integer from a string. So:
C++ Syntax (Toggle Plain Text)
char buf[256]; // Ask for input here cin.getline(buf,256); int num = atoi(buf); if(num == 0) cout << "Error";
Last edited by u8sand; Jul 6th, 2009 at 8:40 pm.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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.
•
•
•
•
The atoi function returns an integer from a string. So:
C++ Syntax (Toggle Plain Text)
char buf[256]; // Ask for input here cin.getline(buf,256); int num = atoi(buf); if(num == 0) // atoi returns 0 if it is invalid cout << "Error";
Last edited by u8sand; Jul 6th, 2009 at 8:42 pm.
•
•
Join Date: Jul 2009
Posts: 22
Reputation:
Solved Threads: 1
•
•
•
•
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?
![]() |
Similar Threads
- Function Pointer with variable number of arguments (C++)
- variable number of arguments (C)
- functions with variable number of arguments (C++)
- check if the user enters a number instead of a letter (C++)
Other Threads in the C++ Forum
- Previous Thread: Random number generators
- Next Thread: question about if/else
Views: 616 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






