1
當我運行該程序時,setprecision
與所有變量一起工作。我試圖讓變量secondsinput
成爲一個整數。我已經嘗試將該變量設置爲一個整數,但是這使得數學運算出來了。有任何想法嗎 ?在使用setprecision()的C++幫助中
using namespace std;
int main()
{
double outputminutes, outputhours, outputdays, secondsinput;
cout << "Please enter the number of seconds: ";
cin >> secondsinput;
cout << endl;
outputminutes = secondsinput/60;
outputhours = secondsinput/3600;
outputdays = secondsinput/86400;
cout << setprecision(2) << fixed;
if (secondsinput > 60 && secondsinput < 3600)
cout << secondsinput << " seconds is "
<< outputminutes << " minutes."
<< endl << endl;
else if (secondsinput > 3600 && secondsinput < 86400)
cout << secondsinput << " seconds is "
<< outputhours << " hours."
<< endl << endl;
else if (secondsinput > 86400)
cout << secondsinput << " seconds is "
<< outputdays << " days."
<< endl << endl;
}
不知何故,我知道和答案是簡單的,但我不知道有足夠的瞭解C++來理解這樣的事情。感謝您的幫助! – Simsyy
如果你不喜歡'static_cast'的冗長性,你可以使用函數式風格強制轉換來進行這種良性轉型:'double(secondsinput)/ 60' – goji