Hi, everybody, I was working on a program where I have to convert Fahrenheit to Celcius but the degree in Fahrenheit can't be less than -60 or more than 200 but I can't figure out how to set a rule like that. Does anybody have any recommendations?
#include<iostream>
using namespace std;
int main()
{
float celsius, fahrenheit;
cout << "Enter the temperature in Fahrenheit : ";
cin >> fahrenheit;
celsius = (5 / 9.0) * (fahrenheit - 32);
cout << "The temperature in Celsius : " << celsius << endl;
return 0;
}
Thank you for the suggestion. I tried this (the code below) and it's kinda working but is it a different process to make sure that the only thing that comes up is "data value out of range." or did I do something wrong?
#include<iostream>
using namespace std;
int main()
{
float celsius, fahrenheit;
cout << "Enter the temperature in Fahrenheit : ";
cin >> fahrenheit;
if ( fahrenheit >= -60 && fahrenheit <= 200)
{
}
else cout << "Data value out of range." << endl;
celsius = (5 / 9.0) * (fahrenheit - 32);
cout << "The temperature in Celsius : " << celsius << endl;
#include<iostream>
usingnamespace std;
int main()
{
float celsius = 0, fahrenheit = 0;
cout << "Enter the temperature in Fahrenheit : ";
cin >> fahrenheit;
if ( fahrenheit >= -60 && fahrenheit <= 200)
{
celsius = (5 / 9.0) * (fahrenheit - 32);
cout << "The temperature in Celsius : " << celsius << endl;
}
else
cout << "Data value out of range, fahrenheit value must be between -60 <-> 200!" << endl;
return 0;
}
Here is a 2nd way with a do/while loop that keeps on asking the user for the next conversion & exits when they hit "n". Just don't hit a letter when you are asked for a Fahrenheit number, else it fails & you have to exit with the X window. I think it has something to do with a letter converting to ascii & the cin>> waiting from the previous cin. I thought the fix for this was "cin.ignore();" but looks like there is something else I need to learn.
Thanks for all the suggestions and help. I ended up using the layout of the first suggestion from SubZeroWins and everything is now working beautifully.