Page 1 of 1

Help with my functions

PostPosted: Wed Feb 01, 2012 2:05 am
by SuperSonic
These functions are supposed to be used to get the seconds, minutes, hours, etc from a number of milliseconds. They are also supposed to return the number that would be displayed on a normal clock. For example, if I was wanting to get how many seconds 6500 milliseconds was, it wouldn't return 65 seconds, instead it would return 5 seconds. Does that make sense? Anyways, something seems to be wrong with my functions so can anyone go through them and find out what's wrong? I will award with a point to the first to solve the problem :D
Code: Select all
int GetMill(int MILL)
{
    int output = MILL;
    int i;
    for(i = 0; i <= MILL; i += 10)
    {
        if(i >= 1000) output -= 1000;
    }
    return output;
}

int GetSec(int MILL)
{
    int output = 0;
    int i;
    int j = 0;
    for(i = 0; i <= MILL; i += 10)
    {
        if(i >= 1000) j++;
    }
    output = j;
    for(j; j > 60; j -= 60)
    {
        output -= 60;
    }
    return output;
}

int GetMin(int MILL)
{
    int output = 0;
    int i;
    int j = 0;
    for(i = 0; i <= MILL; i += 10)
    {
        if(i >= 60000) j++;
    }
    output = j;
    for(j; j > 24; j -= 24)
    {
        output -= 24;
    }
    return output;
}

int GetHour(int MILL)
{
    int output = 0;
    int i;
    for(i = 0; i < MILL; i += 10)
    {
        if(i >= 1440000) output++;
    }
    return output;
}

Re: Help with my functions

PostPosted: Wed Feb 01, 2012 2:38 am
by skydereign
You should really just use modulo (%) and divide. Modulo does what you are doing in the for loop (returns the remainder). It's a useful thing to know, since it allows you to keep the number in bounds. So 65%60 equals 5. And combine it with divide, to get what you want.
Code: Select all
int get_sec (int num_mil)
{
    return (num_mil/1000) % 60;
}

Re: Help with my functions

PostPosted: Wed Feb 01, 2012 3:16 am
by SuperSonic
Great sky thanks +1 :D