a class function
-
I have this program, and I need to find the difference between the start time and end time. My code:
#include #include class StopWatch { public: StopWatch(float hour, float minute, float second); StopWatch(); void difference(StopWatch start_time, StopWatch end_time); int get_hour(); int get_minute(); int get_second(); int reg_time(); int mil_time(); private: int hour, minute, second; }; void main() { int start_time, end_time; char chr; int difference; cout << "This program is a stop watch program. It will store time in hours, minutes, and seconds" << endl; do { StopWatch start_time, end_time, mil_time, reg_time; cout << "Enter Start Time" << endl; start_time.get_hour(); start_time.get_minute(); start_time.get_second(); cout << "Enter End Time" << endl; end_time.get_hour(); end_time.get_minute(); end_time.get_second(); cout << "In Military Time" << endl; cout << " Start Time Entered: "; start_time.mil_time(); cout << " End Time Entered: "; end_time.mil_time(); cout << "In Regular Time" << endl; cout << " Start Time: "; start_time.reg_time(); cout << " End Time: "; end_time.reg_time(); difference(start_time, end_time); cout << "Do you wish to enter another time(Y/N)?" << endl; cin >> chr; if(chr != 'N' && chr != 'n' && chr != 'Y' && chr != 'y') { cout << "Enter Y or N" << endl; } }while (chr == 'Y' || chr == 'y'); } StopWatch::StopWatch(float hours, float minutes, float seconds) { hour = hours; minute = minutes; second = seconds; } StopWatch::StopWatch() : hour(0), minute(0), second(0) { //empty } int StopWatch::get_hour() { cout << " Hour: "; cin >> hour; return hour; } int StopWatch::get_minute() { cout << " Minute: "; cin >> minute; return minute; } int StopWatch::get_second() { cout << " Second: "; cin >> second; return second; } int StopWatch::reg_time() { cout << setfill('0') << setw (2) << hour << ":" << setw (2) << minute << ":" << setw(2) << second; if(hour < 12) cout << " AM"; else cout << " PM"; cout << endl; return 0; } int StopWatch::mil_time() { cout << setfill('0') << setw (2) << hour << ":" << setw (2) << minute << ":" << setw(2) << second << endl; return 0; } void difference(StopWatch start_time, StopWatch end_time) { cout << "difference = " << start_time.get_hour() - end_time.get_hour() << " hours, " << start_time.get_minute() -
-
I have this program, and I need to find the difference between the start time and end time. My code:
#include #include class StopWatch { public: StopWatch(float hour, float minute, float second); StopWatch(); void difference(StopWatch start_time, StopWatch end_time); int get_hour(); int get_minute(); int get_second(); int reg_time(); int mil_time(); private: int hour, minute, second; }; void main() { int start_time, end_time; char chr; int difference; cout << "This program is a stop watch program. It will store time in hours, minutes, and seconds" << endl; do { StopWatch start_time, end_time, mil_time, reg_time; cout << "Enter Start Time" << endl; start_time.get_hour(); start_time.get_minute(); start_time.get_second(); cout << "Enter End Time" << endl; end_time.get_hour(); end_time.get_minute(); end_time.get_second(); cout << "In Military Time" << endl; cout << " Start Time Entered: "; start_time.mil_time(); cout << " End Time Entered: "; end_time.mil_time(); cout << "In Regular Time" << endl; cout << " Start Time: "; start_time.reg_time(); cout << " End Time: "; end_time.reg_time(); difference(start_time, end_time); cout << "Do you wish to enter another time(Y/N)?" << endl; cin >> chr; if(chr != 'N' && chr != 'n' && chr != 'Y' && chr != 'y') { cout << "Enter Y or N" << endl; } }while (chr == 'Y' || chr == 'y'); } StopWatch::StopWatch(float hours, float minutes, float seconds) { hour = hours; minute = minutes; second = seconds; } StopWatch::StopWatch() : hour(0), minute(0), second(0) { //empty } int StopWatch::get_hour() { cout << " Hour: "; cin >> hour; return hour; } int StopWatch::get_minute() { cout << " Minute: "; cin >> minute; return minute; } int StopWatch::get_second() { cout << " Second: "; cin >> second; return second; } int StopWatch::reg_time() { cout << setfill('0') << setw (2) << hour << ":" << setw (2) << minute << ":" << setw(2) << second; if(hour < 12) cout << " AM"; else cout << " PM"; cout << endl; return 0; } int StopWatch::mil_time() { cout << setfill('0') << setw (2) << hour << ":" << setw (2) << minute << ":" << setw(2) << second << endl; return 0; } void difference(StopWatch start_time, StopWatch end_time) { cout << "difference = " << start_time.get_hour() - end_time.get_hour() << " hours, " << start_time.get_minute() -
You would do better for your difference function to return a value that is validly in 0-60 minutes and seconds every time. You would also do better to return the time as a structure that you define, and providing an operator << for it, so that it can then be sent to cout. This class is not very useful or reuseable. I know it's you're homework, but still, best learn good principles now. And that includes not asking broad homework questions. I presume the trouble is that you're getting gaps with negatives in them ? Just think about what it is you need to do to work around that, it's not hard. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
I have this program, and I need to find the difference between the start time and end time. My code:
#include #include class StopWatch { public: StopWatch(float hour, float minute, float second); StopWatch(); void difference(StopWatch start_time, StopWatch end_time); int get_hour(); int get_minute(); int get_second(); int reg_time(); int mil_time(); private: int hour, minute, second; }; void main() { int start_time, end_time; char chr; int difference; cout << "This program is a stop watch program. It will store time in hours, minutes, and seconds" << endl; do { StopWatch start_time, end_time, mil_time, reg_time; cout << "Enter Start Time" << endl; start_time.get_hour(); start_time.get_minute(); start_time.get_second(); cout << "Enter End Time" << endl; end_time.get_hour(); end_time.get_minute(); end_time.get_second(); cout << "In Military Time" << endl; cout << " Start Time Entered: "; start_time.mil_time(); cout << " End Time Entered: "; end_time.mil_time(); cout << "In Regular Time" << endl; cout << " Start Time: "; start_time.reg_time(); cout << " End Time: "; end_time.reg_time(); difference(start_time, end_time); cout << "Do you wish to enter another time(Y/N)?" << endl; cin >> chr; if(chr != 'N' && chr != 'n' && chr != 'Y' && chr != 'y') { cout << "Enter Y or N" << endl; } }while (chr == 'Y' || chr == 'y'); } StopWatch::StopWatch(float hours, float minutes, float seconds) { hour = hours; minute = minutes; second = seconds; } StopWatch::StopWatch() : hour(0), minute(0), second(0) { //empty } int StopWatch::get_hour() { cout << " Hour: "; cin >> hour; return hour; } int StopWatch::get_minute() { cout << " Minute: "; cin >> minute; return minute; } int StopWatch::get_second() { cout << " Second: "; cin >> second; return second; } int StopWatch::reg_time() { cout << setfill('0') << setw (2) << hour << ":" << setw (2) << minute << ":" << setw(2) << second; if(hour < 12) cout << " AM"; else cout << " PM"; cout << endl; return 0; } int StopWatch::mil_time() { cout << setfill('0') << setw (2) << hour << ":" << setw (2) << minute << ":" << setw(2) << second << endl; return 0; } void difference(StopWatch start_time, StopWatch end_time) { cout << "difference = " << start_time.get_hour() - end_time.get_hour() << " hours, " << start_time.get_minute() -
if the user enters 25 and more or the user enters -ve values for hours,the program will not prompt any error message and to find the difference, write a function where all the hours ,minutes and sec are compared with 59 mins , 59 secs convert to next hours and think ur logic bye thennati