C++ 计算表达式后,未将值指定给变量
每次我在Temp中输入Cel的值都保持为零,为什么表达式不在其中存储值C++ 计算表达式后,未将值指定给变量,c++,C++,每次我在Temp中输入Cel的值都保持为零,为什么表达式不在其中存储值 #include<iostream> using namespace std; int main(){ float Far, Cel, Temp; char choice; cout << " To convert from Celcius to Farenheit, press 'f' for opposite 'c': "<<endl; cin &
#include<iostream>
using namespace std;
int main(){
float Far, Cel, Temp;
char choice;
cout << " To convert from Celcius to Farenheit, press 'f' for opposite 'c': "<<endl;
cin >> choice;
cout << "Enter Temperature: " << endl;
cin >> Temp;
if(choice == 'f' || choice == 'F'){
Far = (Temp*(9/5))+32;
cout << "Temperature in Farenheit is: " << Far << endl;
}
else if(choice == 'c' || choice == 'C'){
Cel = (Temp - 32) * (5/9);
cout << "Temperature in Celcius is: " << Cel << endl;
}
return 0;
}
#包括
使用名称空间std;
int main(){
远漂、Cel、Temp;
字符选择;
因为选择是“f”,还是不是“c”?澄清一下,5/9
是0
,所以Cel=…
的RHS上的整个表达式的计算结果是0
。是的,它精确地打印了您键入的内容。哦,您的意思是为了一致性,我应该执行5.0/9.0。为了不重复您自己,这可能会更好做一个你可以重复使用的常数。