How To Set Min and Max For a Conversion?

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;
}
Say
if ( fahrenheit >= ??? && fahrenheit <= ??? )
Replace question marks with something appropriate.
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;

return 0;
}
Well the idea would be to only do the things you want done when the condition is true to actually be inside the { } you created.

Also, use the code tags when you post code.
https://www.cplusplus.com/articles/jEywvCM9/
I am relatively new at this too, but here goes the fix for your way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
using namespace std;

int main()
{

float celsius = 0, fahrenheit = 0;
char userInput = 'n';

do
{
	cout << "Enter the temperature in Fahrenheit: ";
	cin >> fahrenheit;

	
	if (fahrenheit < -60 || fahrenheit > 200)
	{
		cout << "Data value out of range, fahrenheit value must be between -60 <-> 200!" << endl;
	}
	else
	{
		celsius = (5 / 9.0) * (fahrenheit - 32);
		cout << "The temperature in Celsius : " << celsius << endl << endl;	
		cout << "Do you want to calculate another celsius conversion (y/n)?";
		//cin.ignore();
		cin >> userInput;
	}
} while (userInput != 'n');

cout << "GOODBYE!";
	
return 0;
} 
Last edited on
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.
SupremeFonzie, a bit of advice for the future....

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Topic archived. No new replies allowed.