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

Recommended Answers

All 6 Replies

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";
commented: Helped so much ! Thank you ! +0

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:

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.

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?

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!

Thanks for your help - I got it working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.