Patrick
Patrick

Reputation: 19

I tried to run this code but it keeps giving an output of else condition

The string "Fahrenheit" should have given an output of the first if statement, but instead it gives off an output of the else statement.

#include <iostream>

using namespace std;

class Temperature {
public:
    int temp;
    string unit;

    Temperature(int atemp, string aunit) {
        atemp = temp;
        aunit = unit;
    }

    void to_fahrenheit() {
        if (unit == "Fahrenheit") {
            cout << ((temp*1.8) + 32) << " Fahrenheit";
        } else if (unit == "Celsius") {
            cout << ((temp-32)*5/9) << " Celsius";
        } else {
            cout << "Converts only Fahrenheit to Celsius or vice versa.";
        }
    }
};

int main()  {
    Temperature temp1 (10,"Fahrenheit");
    temp1.to_fahrenheit();
    return 0;
}

Upvotes: 0

Views: 82

Answers (6)

Ausaf Ahmad
Ausaf Ahmad

Reputation: 44

Public variables should be assigned a value passed as a parameter in the constructor. So, in Temperature Constructor:

temp = atemp and unit = aunit

Final code :

#include <iostream>
using namespace std;

class Temperature
{
public:
int temp;
string unit;
Temperature(int atemp, string aunit)
{
    temp = atemp;
    unit = aunit;
}
void to_fahrenheit()
{
    if (unit == "Fahrenheit")
    {
        cout << ((temp * 1.8) + 32) << " Fahrenheit";
    }
    else if (unit == "Celsius")
    {
        cout << ((temp - 32) * 5 / 9) << " Celsius";
    }
    else
    {
        cout << "Converts only Fahrenheit to Celsius or vice versa.";
    }
}
};

int main()
{
Temperature temp1(10, "Fahrenheit");
temp1.to_fahrenheit();
return 0;
}

Upvotes: 0

omkaram rangasesha
omkaram rangasesha

Reputation: 21

#include <iostream>
using namespace std;

class Temperature   {
public:
    int temp;
    string unit;
    Temperature(int atemp, string aunit)    {
    //atemp = temp;
    //aunit = unit;
// change the assignment you will get your desired output
    temp = atemp;
    unit = aunit;
    }
    void to_fahrenheit()    {
        if  (unit == "Fahrenheit")   {
            cout << ((temp*1.8) + 32) << " Fahrenheit";
        } else if (unit == "Celsius") {
            cout << ((temp-32)*5/9) << " Celsius";
        } else  {
            cout << "Converts only Fahrenheit to Celsius or vice versa.";
        }
    }
};

int main()  {
    Temperature temp1 (10,"Fahrenheit");
    temp1.to_fahrenheit();
    return 0;
}

Upvotes: 0

ptPham
ptPham

Reputation: 21

your code are wrong at this line.

atemp = temp;
aunit = unit;

Must be:

temp = atemp;
unit = aunit;

Thank

Upvotes: 0

DailyLearner
DailyLearner

Reputation: 2374

Your constructor implementation is wrong, you should assign the input parameters to class member variables rather than other way around:

Temperature(int atemp, string aunit) 
   : temp{atemp}
   , unit{aunit} 
{
}

Upvotes: 0

Scientist
Scientist

Reputation: 17

The problem here is to assign the variables properly.

 Temperature(int atemp, string aunit)    {
temp = atemp;
unit = aunit;
}

In C++, = operator follows right to left assignation.

Upvotes: 0

john
john

Reputation: 88007

Your assignments are the wrong way round

Temperature(int atemp, string aunit)    {
    atemp = temp;
    aunit = unit;
}

should be

Temperature(int atemp, string aunit)    {
   temp = atemp;
   unit = aunit;
}

This a logic error not a syntax error.

The best way to write this code is to use an initialiser list

Temperature(int atemp, string aunit) : temp(atemp), unit(aunit) {
}

That makes it impossible to make the mistake you made.

Upvotes: 1

Related Questions