coded time calcultator, but the hard way i think..
Results 1 to 2 of 2

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

  1. #1

    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 <stdio.h>
    
    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<0) || (time_minutes > 59) || (time_hours > 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<0) || (duration_minutes > 59) || (duration_hours > 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 >=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>=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<10)/*print exta 0 if hours is lesser then 10*/
    	{	
    		if(end_minutes<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<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


    thanks in advance

  2. #2
    Banned
    Join Date
    Jun 2003
    Posts
    1,302
    Yeah there is a lot easier way to code it. It's called perl. hehehe

    Nice little proggy, when I get to a faster connection, I will check it out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides