当我运行程序时,setprecision
适用于所有变量。我试图让变量secondsinput
成为一个整数。我已经尝试将该变量设置为int,但这会搞砸数学。有什么想法吗?
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;
}
答案 0 :(得分:1)
将secondsinput
设为int
,因为这是您希望输入和显示的方式。只需修好数学。例如:
outputminutes = secondsinput / 60.0;
outputhours = secondsinput / 3600.0;
outputdays = secondsinput / 86400.0;
或
outputminutes = static_cast<double>(secondsinput) / 60;
outputhours = static_cast<double>(secondsinput) / 3600;
outputdays = static_cast<double>(secondsinput) / 86400;