I've figured it out but I want to know HOW.
When I do code I try and figure out the math behind it on the side so I know exactly what is going on.
But this time has stumped me. I know what the code is suppose to be, so it has been implemented but understanding HOW it works is driving me bonkers! If anyone can help explain it would be wonderful.

// calculate seconds to days/hours/minutes/seconds

#include <iostream>
using namespace std;        

const int HOURS_IN_DAY = 24;        // it's good form to make
const int MINUTES_IN_HOUR = 60;     // symbolic constants all caps
const int SECONDS_IN_MINUTE = 60;

int main()
{
    long InSeconds, InMinutes, InHours;
    int seconds;
    int minutes;        
    int hours;
    int days;

    cout << "This program will turn your seconds into days, hours, minutes and seconds." << endl;
    cout << "Enter the amount of seconds:__________\b\b\b\b\b\b\b\b\b\b";
    cin >> InSeconds;               // I typically input 31600000 for reference

        // compute in reverse 
        // THIS IS WHERE THE PROBLEMS START

        // compute seconds
        // I see this as 31600000 % 60 = 526666.666666 and the output it gives is 40 seconds. 
        // Yes the % gives you the remainder / number after decimal... I don't see where the 40 comes from?

        seconds = InSeconds % SECONDS_IN_MINUTE ;

        // throw away seconds used in previous statement and convert to minutes 
        // which seconds are thrown away?... and so on throughout this simple program
        InMinutes = InSeconds / SECONDS_IN_MINUTE ;

        // compute  minutes
        minutes = InMinutes % MINUTES_IN_HOUR ;

        // throw away minutes used in previous statement and convert to hours
        InHours = InMinutes / MINUTES_IN_HOUR ;

        // compute hours
        hours = InHours % HOURS_IN_DAY ;

        // throw away hours used in previous statement and convert to days
        days = InHours / HOURS_IN_DAY ;


        cout << "That's " ;
        if (days > 0)
              cout << days << " days " ;
        if (hours > 0)
              cout << hours << " hours " ;
        if (minutes > 0)
              cout << minutes << " minutes " ;
        if (seconds >0)
              cout << seconds << " seconds " ;
        cout << endl ;

        cin.get();
        cin.get();

    return 0;
}

Recommended Answers

All 3 Replies

your confusing the quotient and remainder

31600000 % 60 = 526666.666666

526666.666666 is not the remainder but the quotient to the equation
try small numbers and you'll see how it works

Try experimenting with the following to better understand the relationship between % and /:

#include <stdio.h>

int main () {
    int x = 10, i = 0;
    for (; i < 30; ++i) {
        printf ("%02d %% %d = %d\n", i, x, i % x);
        printf ("%02d / %d = %d\n\n", i, x, i / x);
    }
    return 0;
}

The output of that looks something like:

00 % 10 = 0
00 / 10 = 0

01 % 10 = 1
01 / 10 = 0

02 % 10 = 2
02 / 10 = 0

03 % 10 = 3
03 / 10 = 0

... <snip> ...

26 % 10 = 6
26 / 10 = 2

27 % 10 = 7
27 / 10 = 2

28 % 10 = 8
28 / 10 = 2

29 % 10 = 9
29 / 10 = 2

Yeah right! I understand you may want to figure out how it works behind the scene but I first hit on that blog as I needed a couple of line of code to solve that rather trivial problem and thought it better to look for a more generic solution. Here it is:

#include <time.h> 
    struct tm t;
    t.tm_sec += uiSec; //add a bunch of seconds into struct
    time_t tt = mktime(&t); //mktime does all the computation for you
    cout << t.tm_sec << " seconds " ;
    cout << t.tm_min << " minutes " ;
    cout << t.tm_hour << " hours " ;
    ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.