我正在使用C++編寫我CS類的編程任務,這給我帶來了問題。該任務是編寫一個程序,要求用戶輸入華氏溫度。然後該程序必須輸出該溫度的攝氏溫度和接下來的20華氏溫度的攝氏溫度。這必須使用單獨的函數完成轉換計算以及函數原型。從C++中的單獨函數返回值?
我得到了一切工作,除了我的攝氏度輸出都顯示爲0.我似乎無法找到任何錯誤或找出什麼是錯的...請幫助!
#include <iostream>
#include <iomanip>
using namespace std;
double Celsius(double temp);
int main() {
double fTemp, cTemp;
cout << "Please input a temperature in degrees Fahrenheit: ";
cin >> fTemp;
cout << "F Temp" << " " << "C Temp"<< endl;
cout << "-------------------" << endl;
for(int count=1; count<= 20; count++){
cTemp = Celsius(fTemp);
cout << setw(6) << fTemp << setw(12) << cTemp<< endl;
fTemp++;
}
return 0;
}
//*************Celsius Function****************//
double Celsius(double temp){
double newC;
newC = (temp-32) * (5/9);
return newC;
}
//************************************************
你會驚訝地發現5除以9是0嗎?這是使用整數除法時會發生的情況。 –