Hi, I have a program which is supposed to give the time elapsed from a given time to another given time. There were three functions which were incomplete setTime(), display1Time()and elapsed(). I have created a solution for two of the functions, but found that there are some erros which I do not understand and was wondering if I can get your help to correct these errors and make the program work completely.My code is displayed below:
I have completed the functions for setTime() and display1Time(), but these have caused the following errors:Code:// Incomplete version // Starting code for student tutorial #include <iostream> using namespace std; struct Mytime { int hours; int mins; }; int timecmp( Mytime, Mytime ); // compares two Mytime values // 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 int 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; return ( 0 ); } 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.hours > t2.hours) { return 1; } else { return -1; } } } int setTime(Mytime& t, int h, int m ) { if (h < 0) { return 1; // error code +1 = underflow hours } else if (h > 23) { return 2; // error code +2 = overflow hours } if (m < 0) { return 4; // error code +4 = underflow mins } else if (m > 60) { return 8; // error code +8 = overflow mins } t.hours = h; t.mins = m; return 0; } void display1Time(Mytime t) { if (h > 10) { cout << h << endl; } else { cout << "0" << h << endl; } if (m > 10) { cout << m << endl; } else { cout << "0" << m << endl; } cout << h << ":" << m << endl; } void elapsed(Mytime t1, Mytime t2, Mytime& duration) { setTime(duration,0,0); }
I tried correcting this error by changing the function to void display1Time (int h, int m) but this did not work saying the following:Code:time.cc: In function `void display1Time(Mytime)': time.cc:133: `h' undeclared (first use this function) time.cc:133: (Each undeclared identifier is reported only once time.cc:133: for each function it appears in.) time.cc:141: `m' undeclared (first use this function)
Hope you can help me. Also, I would like to know how to approach the function elapsed().Code:16 jaguar% g++ time.cc time.cc: In function `int main()': time.cc:58: `struct Mytime' used where a `int' was expected time.cc:60: `struct Mytime' used where a `int' was expected time.cc:63: `struct Mytime' used where a `int' was expected
Thank you if you can help.



LinkBack URL
About LinkBacks


