The Code given does the bulk of the working out. The basics of writing any program in c are availabel in a turotial on this site (or elsewhere on the net if you search for "C Tutorial", or "beginner c" or even "Hello World"). So I leave it to you to discover how to investigate how to make all the pieces work, but I am happy to give some pointers on the way....
Rather than recieve input in the form hh:mm:ss, it would be easist to prompt the user for hours, minutes and seconds individually, then you don't have to worry about parsing a String (if it has to be entered in String format, let me know).
An example of how to recieve input from the user (which I assume is where you are getting it from) would be:
-
int hours1;
-
printf("Please enter the number of hours");
-
scanf("%d", &hours1);
-
It would be a good idea to verify that the input they give is reasonable in the context (eg 0<=hours<24), and you can use a similar statement for minutes and seonds.
Once you have recieved hours, minutes and seconds, you can use the methods posted earlier:
-
int total_seconds1=totalSeconds(hours1, minutes1, seconds1);
-
int total_seconds2=totalSeconds(hours2, minutes2, seconds2);
-
int total_difference = total_seconds2 - total_seconds1;
-
-
int hours=getHours(total_difference);
-
total_difference =total_difference -(hours*3600); //take off the seconds we have finished with.
-
-
int minutes=getMinutes(total_difference );
-
int seconds=total_difference - (minutes*60);
-
-
The earlier post shows how to print the result to the screen.
There should now be plenty of code around for you to play with
NOTE: Very few people here will reply by giving out large free chunks of code. Most are more than happy to help, but do not want to do other people's assignments. If you have absolutely no idea about how to attack a problem, your first port of call should be lecturer/tutor/prac supervisor/teacher. Furthermore, in my experience, if you desperately need this for a final exam, something similar must have been covered during the year. If this question appeared in a previous exam (and you have done no c++ this year), then chances are that they did the year it appeared in the exam (unless which language you use for this course is optional, in which case the exam SHOULD cater for that). Keep in mind also that exams are designed to test what you know. If you can write an equivalent program in pascal (or any other language), do it. While you will not get full marks for the question, you will probably get some credit for at least showing you know something (that is you may get 4/5 marks for a question because you showed you understood the problem even if you didn't know the exact fix). In quantitative subjects (that is ones that have a clear right or wrong answer (the opposite being qualitative, which are usually humatities subjects with essay based answers)), the worst answer you can do is to do nothing. Making an attempt at a question (no matter how far off track you are), will often give you some marks, because (even if you are way off track) you may have inadvertently shown that you understand something else. Most people who run courses WANT YOU TO SUCCEED (firstly because it gives them satisfaction in their work, and secondly because tertiary institutions build reputations on success, and so assess how well their teachers are doing compared to other institutions). Basically attempt all questions on exams even if you have no idea what is going on (also never scribble something out so that it can't be seen, if you cross something out and it is still visible, you may get (some) credit if it was (even slightly) correct).
I have (as is my habbit) waffled on here, and you can choose to ignore my advice, but I'm sure most people who have been through the stresses of earning a qualification will agree (in hindsight) that the stress is worth it.
GOOD LUCK IN YOUR EXAMS!!