The timer is not stopping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aravind12345
    New Member
    • Jan 2014
    • 22

    The timer is not stopping

    Hello my problem in this program is that it dose not stop at 0 it also goes to negative numbers please someone fix this

    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    void newline();
    using namespace std;
    int main()
    {
        int hours = 0;
        int minutes = 0;
        int seconds = 0;
        cout<< "Please enter houres: ";
        cin>>hours;
        cout<< "Enter a minutes: ";
        cin>>minutes;
        cout<<"Enter seconds: ";
        cin>>seconds;
        for(;;){
                newline();
                
    cout<< hours <<" : "<< minutes <<" : "<< seconds << endl;
    Sleep(750);
                --seconds;
                if(seconds == 0){
                --minutes;
                seconds = 60;
                }
                if ( minutes == 0){
                       -- hours;
                       minutes = 0 ;
                       }
                if ( hours == 0){
                     hours = 0;
                    }                 
                     
                     }
                     
    return 0;
    }               
    void newline(){
         cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
         }
  • aravind12345
    New Member
    • Jan 2014
    • 22

    #2
    This is a c++ program

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      There's no exit to your for loop.

      Note also that --minutes will produce -1 minutes if minutes was zero to start with.

      I suspect you want to stop your timer if
      (hours && minutes && seconds) == 0
      but I don't see code like that in the loop:

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Line 29: probably want to set minutes to 60.
        Line 32:this doesn't accomplish anything.
        Lines 11-16: you should validate the inputs.

        Comment

        Working...