Help with my functions

Non-platform specific questions.

Help with my functions

Postby SuperSonic » Wed Feb 01, 2012 2:05 am

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;
}
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Help with my functions

Postby skydereign » Wed Feb 01, 2012 2:38 am

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;
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with my functions

Postby SuperSonic » Wed Feb 01, 2012 3:16 am

Great sky thanks +1 :D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest