我的计算出现问题,结果是0。我正在编写一个程序,让用户以小时,分钟和秒为单位输入时间,然后以秒为单位转换并输出时间。无论输入内容如何,答案始终是0。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int hours = 0, min = 0, sec = 0, total = 0;
total = (((hours * 60) * 60) + (min * 60) + sec);
cout << "Please Enter hours, minutes, and seconds" << endl;
cin >> hours >> min >> sec;
cout << "Your time in seconds is" << total;
}
最佳答案
在读取用户输入之前,您正在计算输出。代替:
total = (((hours * 60) * 60) + (min * 60) + sec);
cout << "Please Enter hours, minutes, and seconds" << endl;
cin >> hours >> min >> sec;
采用
cout << "Please Enter hours, minutes, and seconds" << endl;
cin >> hours >> min >> sec;
total = (((hours * 60) * 60) + (min * 60) + sec);
关于c++ - 计算结果意外0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28512198/