Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. a class function

a class function

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    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() -

    C W 2 Replies Last reply
    0
    • A Anonymous

      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() -

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A Anonymous

        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() -

        W Offline
        W Offline
        warlu
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups