简体   繁体   中英

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

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.

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.

your code are wrong at this line.

atemp = temp;
aunit = unit;

Must be:

temp = atemp;
unit = aunit;

Thank

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} 
{
}

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;
}
#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM