将年龄转换为天,小时,分钟和秒

时间:2016-06-16 00:44:21

标签: c++

我遇到了将年龄转换为秒数的问题。它出现了错误的数字和负数。有人可以帮忙吗?

#include <iostream>

using namespace std;

int main()

{
    int years;

    years = years;

    cout << "How many years have you lived: " ;

    cin >> years;

    cout << "You have lived: " << years; cout << " years:" << endl;

    int days = 0.5 + (years * 365.25);

    cout << days; cout << " days" << endl;

    int hours = days * 24;

    cout << hours; cout << " hours" << endl;

    int minutes = hours * 60;

    cout << minutes; cout << " minutes" << endl;

    int seconds = minutes * 60;

    cout << seconds; cout << " seconds" << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

由于溢出问题,您的错误/负秒数。 int 无法保持如此大的数字(秒)。

因此,使用长漫长长漫长代替 int

并且,不要忘记删除该行

years = years;