i writing program figure out elapsed time between 2 given times.
for reason, getting error: expected identifier or 'c' in regards elapsedtime function prototype before main function.
i have tried moving around program , doesn't make difference if locate after t1 , t2 have been declared. what's problem?
thank you
#include <stdio.h> struct time { int seconds; int minutes; int hours; }; struct elapsedtime(struct time t1, struct time t2); int main(void) { struct time t1, t2; printf("enter start time: \n"); printf("enter hours, minutes , seconds respectively: "); scanf("%d:%d:%d", &t1.hours, &t1.minutes, &t1.seconds); printf("enter stop time: \n"); printf("enter hours, minutes , seconds respectively: "); scanf("%d:%d:%d", &t2.hours, &t2.minutes, &t2.seconds); elapsedtime(t1, t2); printf("\ntime difference: %d:%d:%d -> ", t1.hours, t1.minutes, t1.seconds); printf("%d:%d:%d ", t2.hours, t2.minutes, t2.seconds); printf("= %d:%d:%d\n", differ.hours, differ.minutes, differ.seconds); return 0; } struct elapsedtime(struct time t1, struct time t2) { struct time differ; if(t2.seconds > t1.seconds) { --t1.minutes; t1.seconds += 60; } differ.seconds = t2.seconds - t1.seconds; if(t2.minutes > t1.minutes) { --t1.hours; t1.minutes += 60; } differ.minutes = t2.minutes - t1.minutes; differ.hours = t2.hours - t1.hours; return differ; }
your function doesn't define return type:
struct elapsedtime(struct time t1, struct time t2);
struct
not enough define return type. need struct name well:
struct time elapsedtime(struct time t1, struct time t2);
you need assign return value of function something:
struct time differ = elapsedtime(t1, t2);
with working, logic "borrowing" when doing difference backwards:
if(t1.seconds > t2.seconds) // switched condition { --t2.minutes; // modify t2 instead of t1 t2.seconds += 60; } differ.seconds = t2.seconds - t1.seconds; if(t1.minutes > t2.minutes) // switched condition { --t2.hours; // modify t2 instead of t1 t2.minutes += 60; }
as is, if t1
after t2
, hour negative. if assume means end time following day, add 24 hours:
if(t1.hours > t2.hours) { t2.hours+= 24; } differ.hours= t2.hours - t1.hours;
Comments
Post a Comment