-1

I have a constructor in which I am trying to overrun time. So if the user enters 63 seconds, the 60 seconds get passed on to the minute because it is impossible to have 63 seconds in a minute. This is what I have. I need help with the commented section.

Time::Time(int hours, int minutes, int seconds, int millis) {

        /*int add_millis = millis;
        minutes -= add_millis*60000 ;
        millis += add_millis;*/

        int add_seconds = millis / 1000;
        millis -= add_seconds * 1000;
        seconds += add_seconds;

        int add_minutes = seconds / 60;
        seconds -= add_minutes * 60;
        minutes += add_minutes;

        int add_hours = minutes / 60;
        minutes -= add_hours * 60;
        hours += add_hours;

        hours %= 24;
  • There's a modulus operator in C++.... – StoryTeller - Unslander Monica Oct 15 '17 at 05:48
  • I am familiar with that, but I don't think it helps in this situation. – gamefaqs_god Oct 15 '17 at 05:50
  • What kind of help do you need with the commented out section? Is there some input that is not properly handled by the function as is? – n. m. could be an AI Oct 15 '17 at 05:56
  • It is completely unclear what you are trying to do in the commented section. Incidentally, what do you expect to happen if any of the parameters are negative? – Peter Oct 15 '17 at 06:08
  • What is the purpose of the Time object? – zyndor Oct 15 '17 at 06:10
  • The commented section is my attempt to wrap milli around. So, if I was to enter in 501 milliseconds, (That is not possible as there are only 100 milliseconds in a second) it would take 500 of those milliseconds and pass it up to the minute and so forth. For now, I want to ignore negative parameters. The time object takes in time types (hours, mins, secs, milli) and then prints them out. – gamefaqs_god Oct 15 '17 at 06:17
  • @gamefasq_god (That is not possible as there are only 100 milliseconds in a second) .... really ??? – M. Yousfi Oct 15 '17 at 06:27

1 Answers1

0

Firstly, you need to decide if Time is a 'thing'. It doesn't seem to be in your example. What you want instead of an object is a function, which is a better representation of an 'action' that you perform on 'things'.

Your constructor only seems to operate on variables you pass to them by value (i.e. no member variables, no references), which brings us to the second point: any change you make to the parameters, will only be seen locally. You need to pass by reference or otherwise store them somewhere (i.e. member variables).

Thirdly: you want to cascade your changes from smaller to larger units of time, so you only have to wrap each of them once.

With this in mind, your functionality implemented as a function with parameters passed by reference:

#include <cassert>

void ValidateTime(int& hours, int& minutes, int& seconds, int& ms)
{
    assert(ms >= 0);
    assert(seconds >= 0);
    assert(minutes >= 0);
    assert(hours >= 0);

    int add_seconds = ms / 1000;
    ms %= 1000;

    seconds += add_seconds;
    int add_minutes = seconds / 60;
    seconds %= 60;

    minutes += add_minutes;
    int add_hours = minutes / 60;
    minutes %= 60;

    hours += add_hours;
    // TODO: exercise for the reader: roll your hours into days.
}

Usage example:

int main()
{
    int hours = 1;
    int minutes = 125;
    int seconds = 63;
    int ms = 54100;
    ValidateTime(hours, minutes, seconds, ms);

    std::cout << "hours: " << hours << ", minutes: " << minutes << ", seconds: " << seconds << ", ms: " << ms;
}

Prints hours: 3, minutes: 6, seconds: 57, ms: 100

zyndor
  • 1,418
  • 3
  • 20
  • 36