Search code examples
c++functionreturnboolean

why it does not return anything?


I'm doing a programming exercise about converting Fahrenheit to Celsius [C = 5/9 * (F-32)] with some conditions:

  1. create a function toCelsiusByReference which takes a temperature by reference, and returns a bool so : bool toCelsiusByReference(float &temperature);

  2. change the parameter from Fahrenheit to the equivalent Celsius

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

Solution

  • 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";
    ...