# Seconds Converter:

This is a discussion on Seconds Converter: within the C Programming forums, part of the General Programming Boards category; I want to convert seconds into hours, min and seconds. I was able to test it with a small equation ...

1. ## Seconds Converter:

I want to convert seconds into hours, min and seconds. I was able to test it with a small equation turning seconds into hours but now it returns zeros for every entry. I have tried several ways but I can't seem to figure it out. Can anyone point me in the right direction?

Code:
```

#include <stdio.h>

void secCount(int *seconds, int *hours, int *min, int *sec);

int main(int argc, const char * argv[])
{
int sec=0, hours=0, min=0, seconds =0;

{

printf("Enter the time in Seconds");
scanf("%d",&seconds);

secCount(&seconds, &hours, &min, &sec);

printf("The time is equal to %d hours %d min and %d seconds", hours, min, sec);

}

return 0;

}

void secCount (int *seconds, int *hours, int *min, int *sec)

{
int days;
int s = 0;

days = *seconds / 86400; // sets total to hours //
s %= 86400; // sends remainder //
*hours = s / 3600; // sets total to min //
s /= 3600; // sends remainder //
*min = s / 60; // gets min //
*sec = s % 60;      // sets remanding to  sec //

}```

2. Next time, please copy-paste your code as plain text, and with proper indentation, so it will be easier to read.
Code:
```void secCount (int *seconds, int *hours, int *min, int *sec)
{
int s = 0;
...
*hours = s / 3600;```
What is s? What is s / 3600? Why do you even have days or s as variables?

3. Look at time.h for this. It has great constants and stuff to get you going.

http://www.acm.uiuc.edu/webmonkeys/b...uide/2.15.html

4. There are 3600 seconds in an hour. I thought % send the remainder and therefore I would divide the remainder by 3600 to convert it into hours. Same patten follows for the rest of the code.

As for plain text, sorry. I have been told that twice now and I am not to sure how to do it. Common sense tells me it is in word, but when I briefly looked for it I couldn't find it. My compiler uses the font above.
I naturally know what it looks like, but I have never worked with it.

5. Originally Posted by jocdrew21
There are 3600 seconds in an hour. I thought % send the remainder and therefore I would divide the remainder by 3600 to convert it into hours. Same patten follows for the rest of the code.
I should have said "What is the value of s? What is the value of s / 3600?". You have the right idea, but pay attention to what variables you are using. You set s to 0, then try to do calculations (divide and modulo) with it, expecting some non-zero answers for some reason. Perhaps you meant to use a different variable, or perhaps you meant to give s a meaningful value before doing the calculations. Note, I find it a bit easier to go the other direction:
Code:
```*min = *sec / 60  // now min has the correct number of whole minutes (possibly over 60)
*sec %= 60  // sec has the correct remainder of seconds
// do likewise for minutes to hours, hours to days, etc, which will take care of having more than 60 minutes```
The thing I like about going in that order, is you only ever need to know the conversion factor between two adjacent units (i.e. 60 sec in a min, 24 hr in a day). No need to calculate things like 86400 sec in a day.

Originally Posted by jocdrew21
As for plain text, sorry. I have been told that twice now and I am not to sure how to do it. Common sense tells me it is in word, but when I briefly looked for it I couldn't find it. My compiler uses the font above.
My browser has a right-click option labelled "Paste As Plain Text". Perhaps your browser has the same? If not, open up a basic text editor, like notepad, and paste into an empty doc there (which will remove all formatting), then re-copy it from there (now without formatting), and paste into the forum.

6. Usually, your editor will have a Copy button or selection under Edit, from there, choose "As Plain Text", and you'll have it. If you use the default copy method, you'll keep the Word formatting, since Word will assume you are copying from one Word document, into another one, and will want to keep the formatting.

I can't tell you the exact mouse clicks to make; I don't have Word here. But it's there.

I thought % send the remainder and therefore I would divide the remainder by 3600 to convert it into hours. Same patten follows for the rest of the code.
Yes, mod (%) does return the remainder.

Two minutes with a few simple test seconds and a calculator, will confirm the right math to use. Great scott! that will be much faster than posting on a forum, and waiting for an answer.