1

当我运行程序时,它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;
}
4

1 回答 1

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;
于 2013-10-05T21:22:26.390 回答