-1

When I input 36011 it results in the second(s) output being 36011 instead of 11, I made sure the compiler settings were set correctly and everything. I genuinely don't know what else to do, I have to use long long int for this assignment and it's just not working for me when I try to use the mod function in my code.

#include <iostream>

using namespace std;

int main()
{
long long int seconds;

cin >> seconds;

long long int minutes = seconds / 60;
long long int hours = minutes / 60;
long long int days = hours / 24;

cout << "Total seconds: " << seconds << endl;

if(seconds <= 0)
{
    cout << "\nTotal seconds must be greater than zero";
}

else
{
    if(days > 0)
    {
        hours %= 24;
        cout << "\n" << days << " day(s)";
    }
    if(hours > 0)
    {
        minutes %= 60;
        cout << "\n" << hours << " hour(s)";
    }
    if(minutes > 0)
    {
        seconds %= 60;
        cout << "\n" << minutes << " minute(s)";
    }
    if(seconds > 0)
    {
        cout << "\n" << seconds << " second(s)";
    }
}
cout << endl;

return 0;}
Ethan
  • 1
  • 1
    when you input `36011` as `seconds`, your `minutes` variable will hold `600`. Thus, when doing `minutes %= 60` your `minutes` will hold a value of `0`. Because of that, your `if( minutes > 0){ ... }` will not be executed and will not perform modulo operator on `seconds`. Very easy to spot using debugger, I highly encourage you to learn how to do so – Fureeish Oct 08 '17 at 23:18

1 Answers1

0

You are calculating your values the wrong way. Try something more like this instead:

#include <iostream>

using namespace std;

int main()
{
    long long int seconds;
    cin >> seconds;

    if (seconds <= 0)
    {
        cout << "Total seconds must be greater than zero" << endl;
    }
    else
    {
        cout << "Total seconds: " << seconds << endl;

        long long int minutes = seconds / 60; seconds %= 60;
        long long int hours = minutes / 60; minutes %= 60;
        long long int days = hours / 24; hours %= 24;

        /* alternatively:
        long long int days = seconds / 86400; seconds %= 86400;
        long long int hours = seconds / 3600; seconds %= 3600;
        long long int minutes = seconds / 60; seconds %= 60;
        */

        if (days > 0) {
            cout << days << " day(s)" << endl;
        }
        if (hours > 0) {
            cout << hours << " hour(s)" << endl;
        }
        if (minutes > 0) {
            cout << minutes << " minute(s)" << endl;
        }
        if (seconds > 0) {
            cout << seconds << " second(s)" << endl;
        }
    }

    return 0;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620