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