Hi. Whats wrong here?
Code:hour = total_sec /3600; min = total_sec % 60; sec = min % 60;
Hi. Whats wrong here?
Code:hour = total_sec /3600; min = total_sec % 60; sec = min % 60;
Your calculation of min doesn't really bear any resemblance to what a minute is.
Say you're given an integer 123, and your task is to extract each digit using /10 and %10
Well it's exactly the same, except hms uses /60 and %60
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
explain please? I thought 3600/60= 60.
It is. Is that relevant? No. (EDIT: A hint, because I'm feeling generous. 120 seconds is 2 minutes. What is 120%60?)
Last edited by tabstop; 05-26-2011 at 11:31 PM.
Yes.true & it was working for me when I was just doing minutes & seconds, but when I included hours it didn't.like so
Code:#include <stdio.h> int main() { int input_v,hour,min,sec; printf("input number in total seconds\n"); scanf("%d", &input_v); while(input_v > 3600){ hour = input_v /3600; min = input_v % 60; sec = min % 60; printf("%dhours%dminutes and %dseconds\n",hour,min,sec); } min = input_v / 60; sec = input_v % 60; printf("%dminutes and %dseconds\n",min,sec); return 0; }
Last edited by anaer0bic; 05-26-2011 at 11:38 PM.
That's because you did something completely different (and, coincidentally, correct). Compare your correct version on line 15 with your not-at-all correct version on line 11.
cheers tabstop
So this works with the while statement commented out. Whats wrong with the while?
Code:#include <stdio.h> int main() { int input_v,hour,min,sec; printf("input total seconds\n"); scanf("%d", &input_v); hour=input_v/3600; min=(input_v%3600)/60; sec=(input_v%60)%60; /*while(hour<2){ printf"%dhour %dminutes %dseconds\n",hour,min,sec); }*/ printf("%dhours %dminutes %dseconds\n",hour,min,sec); return 0; }
How is hour going to change inside the loop? If you get in, you can never get out. You probably meant "if" instead.
Hey guys. I'm new here and new on C.
Im also trying to convert seconds to hours, minutes, seconds.
I got this but i cant seem to make it work. I can compile it but it exits there's really something wrong. Can anyone point out the problem or can give a hint.
Thanks for any reply.Code:#include <stdio.h> main( ) { int input, hr, min, sec; printf("Input Value: "); scanf ("%d", &input); hr = (input/3600); min =(input%3600)/60; sec = (input%60)%60; printf ("%d Hours, %d Minutes, %d seconds", hr, min, sec); }
Once a program is finished, it always exits; that's just the way programs work. So once it prints the Hours, Minutes, and Seconds, there's nothing left for it to do, so it stops. You should be running this program from the console anyway so that you can see the output.
(Also: This thread wasn't extremely dead, but in general you make a new thread and maybe make a link to the old one if you need to refer to it. In this case you don't refer to anything that comes before, so putting it in an old thread isn't useful.)
Couple of preliminary things:
1) If you have a new question, you might as well start a new thread. If you think it relates substantially to some other thread, add a link.
2) the return type of main is int, and it should do so:
Code:int main (void) { return 0; }
Beyond that, your code is fine, I ran it, it compiles without errors (but there is a warning about the return type of main) and it works. I suspect you are working on MS Windows, and by "exit" you mean the window disappears. Add something like this at the end:
Now you have to enter q (and return) to end the program.Code:int c = 0; while (c != 'q') c = getchar();
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Read the forum guidelines. You shouldn't be posting to a thread more than two weeks old. Make your own next time.
It's int main(void), and return an int at the end, usually 0 for success.
Lastly, your code works, apart from the int main thing. There's not really something wrong, you just need to keep the console window open until you're done. Read this: Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?
Thank you tabstop, MK27 and anduril462.
I'm so stupid. I didn't know about that console disappearing. Thank you for the inputs it was an enlightenment
I'm sorry about bumping the topic. I'll be reading the guidelines before posting again.