Time class member function help!

I am creating a class called time and one of my constructors has to take in a parameter purely in seconds and output it as days:hours:minutes:seconds.
I cannot seem to get it figured out. I started out a for loop with if statements inside but they do not work at all. Any help would be awesome. The variables day, hour, minute, second are all private member data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

class TimeFormatter
{
private:
    int seconds;
    int minutes;
    int hours;
    int days;
public:
    TimeFormatter(int sec)
    {
        seconds = sec;
        days = seconds / 86400;  //86,400 seconds in a day
        seconds %= 86400;  //Use modulus for the remainder.
                           //Once we take out all the seconds that
                           //can constitute a full day, this will
                           //give us the number of seconds we have left
                           //to allocate to other units of time.

        hours = seconds / 3600;  //3600 seconds in an hour
        seconds %= 3600;

        minutes = seconds / 60;  //60 seconds in a minute
        seconds %= 60;
    }
    void printFormatted()
    {
        std::cout << days << ':' << hours << ':' << minutes << ':' << seconds;
    }
};

int main()
{
    int seconds = 2000000;
    std::cout << "Number of seconds: " << seconds << '\n';

    TimeFormatter tf(seconds);
    tf.printFormatted();

    return 0;
}
Last edited on
its not seeming to work right

1
2
3
4
5
6
7
8
9
10
11
time::time(int sTotal)
{
    Days = totalSeconds / 86400;
    Seconds %= 86400;
    Hours = totalSeconds / 3600;
    Seconds %= 3600;
    Minutes = totalSeconds / 60;
    Seconds %= 60;

}


I put in 10 as a parameter and it ouputted 0:0:0:55

I am not sure if there is something wrong with my code or its because I can't seem to get my error checking right either where days can be whatever but if hours gets over 24 then days go up and minutes or seconds over 60 then the preceding goes up. I have no clue how to get that error checking right.
What is sTotal being passed in for if it is never used inside the function? At the very beginning of that method, try Seconds = sTotal; then start doing all the math. And what is totalSeconds?
1
2
3
4
5
6
7
8
9
10
11
time::time(int sTotal)
{
    Seconds = sTotal;
    Days = Seconds / 86400;
    Seconds %= 86400;
    Hours = Seconds / 3600;
    Seconds %= 3600;
    Minutes = Seconds / 60;
    Seconds %= 60;

}
Last edited on
i originally had it as totalSeconds instead of sTotal. I just forgot to change it back. It works for everything under 3600. but 3600 will yield 0:1:60:0
and 86400 yields 1:24:1440:0
Yes, because you're dividing out of the wrong value.
This is wrong:
1
2
3
4
5
6
7
8
9
10
11
time::time(int sTotal)
{
    Seconds = sTotal;

    Days = sTotal/ 86400;
    Seconds %= 86400;
    Hours = sTotal/ 3600;
    Seconds %= 3600;
    Minutes = sTotal/ 60;
    Seconds %= 60;
}


This is correct:
1
2
3
4
5
6
7
8
9
10
11
time::time(int sTotal)
{
    Seconds = sTotal;

    Days = Seconds / 86400;
    Seconds %= 86400;
    Hours = Seconds / 3600;
    Seconds %= 3600;
    Minutes = Seconds / 60;
    Seconds %= 60;
}
ohhhhhhh! okay I see what you're saying! Yep that made it work perfectly! Thank you!

You wouldn't have any suggestions for my other constructor that takes in 4 parameters int d,h,m,s and I need to get it where if you input something like

time t1(4,10,3,61)

it would change to 4:10:4:1

I am just lost on the error checking for it. Would it be similar to this one?
Sure. You could work in the other direction. Get all the overflow out of your seconds by pushing it onto your minutes. If you have too many minutes, do the same thing. Just keep pushing the extra time up to your biggest time unit, days in this case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
time::time(int d, int h, int m, int s) : days(d), hours(h), minutes(m), seconds(s)
{
    if(seconds >= 60)
    {
        minutes += (seconds / 60);
        seconds %= 60;
    }

    if(minutes >= 60)
    {
        hours += (minutes / 60);
        minutes %= 60;
    }

    if(hours >= 24)
    {
        days += (hours / 24);
        hours %= 24;
    }
}
How come none of the parameter variables are used?
yea my output is throwing out crazy weird numbers
They are, perhaps you haven't seen that constructor syntax before. I use them to construct the member variables:
time::time(int d, int h, int m, int s) : days(d), hours(h), minutes(m), seconds(s)

If it is more familiar and helpful to you, just consider it functionally the same as this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
time::time(int d, int h, int m, int s)
{
    days = d;
    hours = h;
    minutes = m;
    seconds = s;

    if(seconds >= 60)
    {
        minutes += (seconds / 60);
        seconds %= 60;
    }

    if(minutes >= 60)
    {
        hours += (minutes / 60);
        minutes %= 60;
    }

    if(hours >= 24)
    {
        days += (hours / 24);
        hours %= 24;
    }
}
Yea man I am only familiar with the latter. My OOP teacher hasn't ever shown us how to it the first way you did it. But by looking at it I can see what it means. Is the way you do it a better and more acceptable way? I am assuming it is since it takes 4 less lines of code to write it
Yes, it's better, because it initialises those data members with those values at construction time, rather than constructing them and then assigning values after construction.

It becomes more important when your class has other classes as members.
Oh okay that makes sense! Then I am sure it will be coming up soon then since we are just about to get into other classes becoming class variables. Thank you again for your help. It is greatly appreciated
Topic archived. No new replies allowed.