If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

coded time calcultator, but the hard way i think..

hi guys,

i'm still learning C, and one of the assignments of a tutorial was to code a time calculator, well i did, and it works fine, but i think it can be done a lot easier, just don't know how, perhaps there is some library for it...

here's the source:

Code:

#include &lt;stdio.h&gt;
int main()
{
int time,time_hours,time_minutes;
int duration,duration_hours,duration_minutes;
int end_hours,end_minutes;
time_minutes=61;/*make sure the while loop will start to run*/
time_hours=25;/*make sure the while loop will start to run*/
duration_minutes=61;/*make sure the while loop will start to run*/
duration_hours=25;/*make sure the while loop will start to run*/
printf("\nThis program allows you to calculate the end time");
printf("\nwhen you enter the start time and duration.");
printf("\nUsage: 1234 is 12 hours,34 minutes, so always enter");
printf("\nyour times like this.\n\n");
while((time&lt;0) || (time_minutes &gt; 59) || (time_hours &gt; 23))/*bounds check*/
{
printf("\nEnter the start time: ");
scanf("%d",&time);
time_minutes=time%100;/*calculate time hours and minutes*/
time_hours=(time-time_minutes)/100;
}
while((duration&lt;0) || (duration_minutes &gt; 59) || (duration_hours &gt; 23))/*bounds check*/
{
printf("\nEnter the duration: ");
scanf("%d",&duration);
duration_minutes=duration%100;/*calculate duration hours and minutes*/
duration_hours=(duration-duration_minutes)/100;
}
end_minutes=time_minutes+duration_minutes;/*calculate result*/
end_hours=time_hours+duration_hours;
if(end_minutes &gt;=60)/*check if time minutes + duration minutes exceed the 60 minute limit*/
{ /*if so, then add 1 hour for 60 minutes*/
end_minutes-=60;
end_hours++;
}
if(end_hours&gt;=24)/*check if time hours + duration hours exceed the 24 hours, if so*/
{ /*then start counting from 0 again*/
end_hours-=24;
}
if(end_hours&lt;10)/*print exta 0 if hours is lesser then 10*/
{
if(end_minutes&lt;10)/*print extra 0 if minutes are lesser then 10 and hours also*/
printf("\nStarttime is %d. Duration is %d. Endtime is 0%d0%d.\n",time,duration,end_hours,end_minutes);
else /*print extra 0 for the hours, but not for the minutes*/
printf("\nStarttime is %d. Duration is %d. Endtime is 0%d%d.\n",time,duration,end_hours,end_minutes);
char x;/*just to keep the result visible when this program is running in windows*/
x=getch();
return 0;
}
if(end_minutes&lt;10)/*print extra 0 for the minutes, but not for the hours*/
printf("\nStarttime is %d. Duration is %d. Endtime is %d0%d.\n",time,duration,end_hours,end_minutes);
char x;/*just to keep the result visible when the program is running in windows*/
x=getch();
return 0;
}

i've attached the source and executables (both for linux and windows) so you can take a look yourself