计算结果意外0

时间:2015-02-14 03:44:45

标签: c++

我的计算有问题,结果是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;
}

1 个答案:

答案 0 :(得分:2)

在读取用户输入之前,您正在计算输出。而不是:

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);