Question

In this program Iam trying to take 78 degrees Fahrenheit and return them in a class with the Celsius version and kelvin. But for some odd reason I'm just getting this as the output. What am I doing wrong?

This is my output.

78
0
273.15
#include <iostream>
using namespace std;


class Temperature
{
public:

    double getTempKelvin();
    double getTempFahrenheit();
    double getTempCelcius();

    void setTempKelvin(double k);
    void setTempFahrenheit(double f);
    void setTempCelcius(double c);

private:
    double kelvin, fahrenheit, celcius;
    double c, f, k;
};

int main ()
{
    double c, f, k;
    Temperature Conv;

    Conv.setTempFahrenheit(f);
    Conv.setTempCelcius(c);
    Conv.setTempKelvin(k);
    cout << Conv.getTempFahrenheit() << endl;
    cout << Conv.getTempCelcius() << endl;
    cout << Conv.getTempKelvin() << endl;



    return 0;
}

void Temperature::setTempFahrenheit(double f)
{
    f = 78;
    fahrenheit = f;
}

void Temperature::setTempCelcius(double c)
{
    c = (5/9) * ( f - 32);
    celcius = c;
}

void Temperature::setTempKelvin(double k)
{
    k = c + 273.15;
    kelvin = k;
}




double Temperature::getTempFahrenheit()
{
    return fahrenheit;
}

double Temperature::getTempCelcius()
{
    return celcius;
}


double Temperature::getTempKelvin()
{
    return kelvin;
}
Was it helpful?

Solution

5/9 is integer division and will result in 0. You need to use doubles, Try:

void Temperature::setTempCelcius(double c)
{
    c = (5.0/9.0) * ( f - 32);
    celcius = c;
}

OTHER TIPS

Aside from the 5/9 issue, you have three sets of variables called 'c', 'f', and 'k'. One set are the member variables in the class. Another set are the variables in main. The third set are the parameters inside the various get* functions.

It's not clear what purpose the variables in main serve, why the functions take parameters at all, or why your class has two sets of variables for the temperatures (both c and celsius, and so on) but if you give the sets of variables different names, it will become easier to understand why your program isn't working.

Seems that my problem was that i was clearning the k c and f double so i just removed them from the functions.

#include <iostream>
using namespace std;
double c, f, k;
class Temperature
{
public:

    double getTempKelvin();
    double getTempFahrenheit();
    double getTempCelcius();

    void setTempKelvin();
    void setTempFahrenheit();
    void setTempCelcius();

private:
    double kelvin, fahrenheit, celcius;
    double c, f, k;
};

int main ()
{
    Temperature Conv;

    Conv.setTempFahrenheit();
    Conv.setTempCelcius();
    Conv.setTempKelvin();
    cout << Conv.getTempFahrenheit() << endl;
    cout << Conv.getTempCelcius() << endl;
    cout << Conv.getTempKelvin() << endl;



    return 0;
}

void Temperature::setTempFahrenheit(){
    f = 78;
    fahrenheit = f;
}

void Temperature::setTempCelcius()
{
    c = (5.0/9.0) * ( f - 32);
    celcius = c;
}

void Temperature::setTempKelvin()
{
    k = c + 273.15;
    kelvin = k;
}




double Temperature::getTempFahrenheit()
{
    return fahrenheit;
}

double Temperature::getTempCelcius()
{
    return celcius;
}


double Temperature::getTempKelvin()
{
    return kelvin;
}
#include<iostream>
using namespace std;
class temperature
{
    public :
    virtual void calculate(float)=0;
}; 
class ftoc : public temperature
{
    public :
    float c;
    void calculate(float f)
    { 
        c=(f-32)*5/9;
        cout<<"Temperature in celcius is : "<<c<<" `C "<<endl;
    }
};
class ftok : public temperature
{
    public : 
    float k;
    void calculate(float f)
    {
        k=(f+459.67)*5/9;
        cout<<"Themperature in kelvin is : "<<k<<" K "<<endl;
    }
};
int main()
{
    float f;
    ftoc a;
    ftok b;
    cout<<"Enter the temperature : ";
    cin>>f;
    a.calculate(f);
    b.calculate(f);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top