[英]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.