954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Checking if variable is a number or a letter: Error

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!

#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;
}
Pokenerd
Light Poster
37 posts since Jul 2009
Reputation Points: 10
Solved Threads: 2
 

The atoi function returns an integer from a string. So:

char buf[256];
// Ask for input here
cin.getline(buf,256);
int num = atoi(buf);
if(num == 0)
    cout << "Error";
u8sand
Junior Poster
131 posts since Dec 2008
Reputation Points: 78
Solved Threads: 15
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

The atoi function returns an integer from a string. So:

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";

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.

u8sand
Junior Poster
131 posts since Dec 2008
Reputation Points: 78
Solved Threads: 15
 
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?

Pokenerd
Light Poster
37 posts since Jul 2009
Reputation Points: 10
Solved Threads: 2
 

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!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

Thanks for your help - I got it working!

Pokenerd
Light Poster
37 posts since Jul 2009
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You