Reputation:
I'm doing a programming exercise about converting Fahrenheit to Celsius [C = 5/9 * (F-32)] with some conditions:
create a function toCelsiusByReference which takes a temperature by reference, and returns a bool so : bool toCelsiusByReference(float &temperature);
change the parameter from Fahrenheit to the equivalent Celsius
return true if the parameter was above freezing (>32 F), return false
I did 1 and 2 and I'm stuck with 3 which does not return me anything. I do not understand why?
I'm testing the value 60 F as temperature which should return me true since 60 F > 32 F.
Why my function bool toCelsiusByReference(float &temperature)
does not return me anything
Here is the code :
#include <iostream>
#include <iomanip>
using namespace std;
bool toCelsiusByReference(float &temperature);
int main()
{
float temperature = 60;
toCelsiusByReference(temperature);
return 0;
}
bool toCelsiusByReference(float &temperature)
{
float celsius;
bool status;
// convert celsius to Fahrenheit
cout.setf(ios::fixed, ios::floatfield);
celsius = 5.00 / 9.00 * (temperature - 32);
// cout << "Degrees C : " << setprecision(2) << celsius << endl;
// check if temperature (fahrenheit) is freezing (<32) or not
if (temperature > 32)
{
status = true;
}
else
{
status = false;
}
return status;
}
Upvotes: 0
Views: 159
Reputation: 1205
Short answer:
Store value returned from function
int main
{
...
bool b = toCelsiusByReference(...)
}
Upvotes: 1
Reputation: 50778
Basic knowledge: you need to use the returned value somehow.
...
if (toCelsiusByReference(temperature))
{
cout << "above 32°F\n";
}
else
{
cout << "below 32°F\n";
}
cout << "Converted temperature: " << temperature << " °C\n";
...
Upvotes: 1
Reputation: 52185
In your case, it would appear that you are not storing what the function (toCelsiusByReference
) returns: toCelsiusByReference(temperature);
.
Now, from a coding perspective, I'd recommend some changes. Try to keep your methods as simple as possible. In your case, you are doing a temparature check in your conversion mechanism, which, at least in my opinion, shouldn't be there.
This also makes the name of the method a bit misleading, since true
or false
isn't what one would expect from a method called toCelsiusByReference
.
So in short:
toCelsiusByReference
method, return the equivalent value in degrees celcius.Upvotes: 2