Home > Blockchain >  I am new to C . I tried to run this code but it keeps giving an output of else condition. Is there
I am new to C . I tried to run this code but it keeps giving an output of else condition. Is there

Time:06-06

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

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

your code are wrong at this line.

atemp = temp;
aunit = unit;

Must be:

temp = atemp;
unit = aunit;

Thank

CodePudding user response:

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

I hope you understood. Please upvote if you like this!

CodePudding user response:

#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;
}
  •  Tags:  
  • c
  • Related