Thread: Functions! Need help ASAP plz!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Functions! Need help ASAP plz!

    Ok there are seven fuctions. This program calculates the different between two time intervals. The fuction "getTime" has to be called twice by main, once to get the start time, and then again to get the end time. But how can i use that function to do both??? Also, the program runs, but it doesn't calculate anything. Any help is appreciated.

    Code:
    #include <stdio.h>
    
    void getTime (int* h, int* m, int* s);
    void duration (int h, int m, int s, int endh, int endm, int ends, float* totalhour, float* totalmin);
    int calcDuration (int h, int m, int s, int endh, int endm, int ends);
    float toHour (int totalsec);
    float toMin (int totalsec);
    void printDuration (float* totalhour, float* totalmin);
    
    int main(void)
    
    {
    	int h, m, s, endh, endm, ends;
    	float totalhour, totalmin;
    
    	getTime (&h, &m, &s);
    	getTime (&endh, &endm, &ends);
    	duration (h, m, s, endh, endm, ends, &totalhour, &totalmin);
    	printDuration (&totalhour, &totalmin);
    
    	return 0;
    }
    
    // --------------------------------------------------------------------------------------------
    
    void getTime (int* h, int* m, int* s)
    
    {
    	printf ("Enter time (hh:mm:ss): ");
    	scanf ("%d:%d:%d", h, m, s);
    
    	return;
    }
    
    
    // -------------------------------------------------------------------------------------------
    
    
    void duration (int h, int m, int s, int endh, int endm, int ends, 
    			   float* totalhour, float* totalmin)
    {
    	int totalsec;
    
    	totalsec = calcDuration (h, m, s, endh, endm, ends);
    	*totalhour = toHour (totalsec);
    	*totalmin = toMin (totalsec);
    
    	return;
    }
    
    // ---------------------------------------------------------------------------------------------
    
    int calcDuration (int h, int m, int s, int endh, int endm, int ends)
    
    {
    	int startsec, endsec, totalsec;
    	int c = 3600;
    	int b = 60;
    
    	startsec = (h * c) + (m * b) + s;
    	endsec = (endh * c) + (endm * b) + ends;
    
    	totalsec = endsec - startsec;
    	
    	return totalsec;
    }
    
    // ---------------------------------------------------------------------------------------------
    
    float toHour (int totalsec)
    
    {
    	int c = 3600;
    	float totalhour;
    
    	totalhour = (float) totalsec / c;
    
    	return totalhour;
    }
    
    // --------------------------------------------------------------------------------------------
    
    float toMin (int totalsec)
    
    {
    	int c = 60;
    	float totalmin;
    
    	totalmin = (float) totalsec / c;
    
    	return totalmin;
    }
    
    //----------------------------------------------------------------------------------------------
    
    void printDuration (float* totalhour, float* totalmin)
    
    {
    	printf ("The time in hours is: %9.2f\n", &totalhour);
    	printf ("The time in minutes is: %7.2f\n", &totalmin);
    
    	return; 
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    nevermind. I figured it out. thx anywayz

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Just as a matter of interest, why are you using floats to store/display the number of seconds (which you input as integers) and what happens if the start time is greater than the end time? (e.g. if start time is just before midnight and end time is just after)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM