Hi im a little bit stuck on this program im working on.
im creating a program which uses the data type Mytime the output should look something like
Enter start hh mm : 7 30
Enter finish hh mm : 7 30
Time elapsed from 07:30 until 07:30 is 00:00
2 functions have not yet been defined display1Time() & elapsed(). The program currently compiles in unix g++ i have filled the un defined function to get the program running. Basically i cant work out how to get the time displayed and the total time elapsed working. I think the time the user enters has to be converted to minutes. Can anyone help please ?
Code:// Incomplete version #include <iostream> using namespace std; struct Mytime { int hours; int mins; }; int timecmp(Mytime,Mytime); // returns 0 if times are same // returns >0 if first is later than second // returns <0 if first is before second int setTime(Mytime& t, int h, int m ); // if valid time sets hh and mm to h and m // and returns 0 // if invalid returns integer > 0 as error code // error code +1 = underflow hours // error code +2 = overflow hours // error code +4 = underflow mins // error code +8 = overflow mins void display1Time(Mytime t); // displays in form hh:mm void elapsed(Mytime t1, Mytime t2, Mytime& duration); // determines the time duration between t1 and t2 // t1 assumed to be earlier than t2 void main() { Mytime now; Mytime then; Mytime howlong; int h,m; do // validate input { cout << "Enter start hh mm : "; cin >> h >> m ; } while (setTime(now,h,m)); do { cout << "Enter finish hh mm : "; cin >> h >> m ; } while (setTime(then,h,m)); elapsed(now,then,howlong); cout << "Time elapsed from "; display1Time(now); cout << " until "; display1Time(then); cout << " is "; display1Time(howlong); cout << endl; } int timecmp(Mytime t1,Mytime t2) { if (t1.hours == t2.hours) { if (t1.mins == t2.mins) { return 0; } else // mins not same { if (t1.mins > t2.mins) { return 1; // greater than zero } else { return -1; } } } else // hours not same { if (t1.mins > t2.mins) { return 1; } else { return -1; } } } int setTime(Mytime& t, int h, int m ) { int errorCode = 0; if (h < 0) errorCode += 1; if (h > 23) errorCode += 2; if (m < 0) errorCode += 4; if (m > 59) errorCode += 8; if (errorCode==0){t.hours = h; t.mins = m;} return errorCode; } //////////// THE FOLLOWING FUNCTIONS ARE INCOMPLETE void display1Time(Mytime t) { cout << "00:00"; } void elapsed(Mytime t1, Mytime t2, Mytime& duration) { setTime(duration,0,0); }



LinkBack URL
About LinkBacks


