简体   繁体   English

我试图运行这段代码,但它不断给出 else 条件的输出

[英]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.字符串"Fahrenheit"应该给出第一个if语句的输出,但它却给出了else语句的输出。

#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.在 C++ 中,= 运算符遵循从右到左的赋值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试运行代码时,我得到的第二个输出是垃圾值。 谁能告诉那个代码错误 - When I tried to run the code I was getting the 2nd output as garbage value. Can anyone tell fault in that code 代码有一个 if else 条件,但代码没有检查我认为的条件 - code has an if else condition but the code is not checking the conditon I think 如果是,否则代码只解析为else条件 - If else if, code only resolves to else condition C ++ if else语句继续执行else和else下的代码 - C++ if else statement keeps executing the if and not the code under the else 我正在尝试在矩阵中打印最大增加路径,但代码不断给出运行时错误 - I am trying to print the max increasing path in a matrix but the code keeps on giving runtime error 为什么我的代码在 C++ 中运行“while 循环”时会导致问题? 在 if else 语句中,它不断打印错误的 output - Why is my code causing problem while running the "while loop" in C++? in the if else satement it keeps printing the wrong output constantly 为什么我的代码在运行时给我一个stackdump错误? - Why my code giving me a stackdump error when I run it? 为什么这段代码没有给出所需的输出? - why is this code not giving the desired output? 如果我采用 A = 10 ^ 9 和 B = 10 ^ 9,则以下代码给出负 output。 为什么? 对于小整数,它给了我正确的 output - if i am taking A = 10^9 and B=10^9 than following code is giving negative output. why? It is giving me correct output for small integers C ++代码在for循环的情况下给出了运行时错误,而如果用具有相同意义的代码替换,则它可以正确编译 - C++ code is giving a run-time error in the condition of for loop Whereas it is compiling correctly if it is replaced by code which makes same sense
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM