What's new

C/C++ Clocks [Countdown, Stop Watch, Interval]

  • Thread starter DatBoiJay53
  • Start date
  • Views 1,487
DatBoiJay53

DatBoiJay53

Enthusiast
Messages
634
Reaction score
120
Points
135
Sin$
0
I was bored and practicing my programming. Thought it would be useful for people to see :tongue: if you have any questions, feel free to ask. :biggrin:

Clocks.h

C:
#ifndef TIMER_CLOCK_H
#define TIMER_CLOCK_H

class CountdownClock
{
protected:
    int mi_clockMinutes;
    int mi_clockSeconds;
    int mi_clockHours;
    int mi_beap;
    bool mb_isClockActive;
    bool mb_areTimesValid;
public:
    CountdownClock() = default;
    void printCountdownClock();
};

class IntervalClock : public CountdownClock
{
private:
    int mi_clockInterval;
public:
    IntervalClock() = default;
    void printIntervalClock();
};

class StopWatchClock : public CountdownClock
{
public:
    StopWatchClock() = default;
    void printStopwatchClock();
};

#endif

Clocks.cpp
C:
void CountdownClock::printCountdownClock()
{
    cout << "// Countdown Clock Selected \\" << endl << endl;
    int beap = 4;
    bool isClockActive = true;
    bool areTimesValid = false;

    while ( !areTimesValid )
    {
        cout << "Hours: ";
        cin >> mi_clockHours;

        cout << "Minutes: ";
        cin >> mi_clockMinutes;

        cout << "Seconds: ";
        cin >> mi_clockSeconds;

        if ( mi_clockHours < 0 || mi_clockMinutes < 0 || mi_clockSeconds < 0 )
        {
            cout << "Invalid Time" << endl;
            Sleep(1000);
            system("CLS");
        }
        else if ( mi_clockMinutes > 60 || mi_clockSeconds > 60 )
        {
            cout << "Invalid Time (minutes and seconds cannot be over 60)" << endl;
            Sleep(1500);
            system("CLS");
        }
        else
            areTimesValid = true;
    }


    while ( isClockActive )
    {
        cout << "// Countdown Clock Selected \\" << endl << endl;

        if ( mi_clockHours == 0 && mi_clockMinutes == 0 && mi_clockSeconds == 0 )
            break;

        cout << mi_clockHours << ":" << mi_clockMinutes << ":" << mi_clockSeconds << flush;
        Sleep(1000);
        --mi_clockSeconds;

        // Handles minutes and seconds
        if ( mi_clockSeconds == 0 && mi_clockMinutes > 0)
        {
            --mi_clockMinutes;
            mi_clockSeconds = 60;
        }

        // Handles minutes and hours
        if ( mi_clockMinutes == 0 && mi_clockHours > 0 )
        {
            --mi_clockHours;
            mi_clockMinutes = 59;

        }

        if ( mi_clockSeconds < 0 )
            mi_clockSeconds = 60;

        //if (mi_clockHours )

        system("CLS");
        if ( mi_clockHours == 0 && mi_clockMinutes == 0 && mi_clockSeconds == 0 )
            isClockActive = false;
    }

    cout << "DONE" << flush;
    while ( beap )
    {
        cout << "\a" << flush;
        Sleep(500);
        --beap;
    }
}

C:
void IntervalClock::printIntervalClock()
{
    cout << "// Interval Clock Selected \\" << endl << endl;
    mi_beap = 4;
    mb_isClockActive = true;
    mb_areTimesValid = false;

    while ( !mb_areTimesValid )
    {
        cout << "Hours: "<< flush ;
        cin >> mi_clockHours;

        cout << "Minutes: " << flush;
        cin >> mi_clockMinutes;

        cout << "Seconds: " << flush;
        cin >> mi_clockSeconds;

        cout << "Interval: " << flush;
        cin >> mi_clockInterval;


        if ( mi_clockHours < 0 || mi_clockMinutes < 0 || mi_clockSeconds < 0 || mi_clockInterval < 0 || mi_clockInterval > 60 )
        {
            cout << "Invalid Time" << endl;
            Sleep(1000);
            system("CLS");
        }
        else if ( mi_clockMinutes > 60 || mi_clockSeconds > 60 )
        {
            cout << "Invalid Time" << endl;
            Sleep(1000);
            system("CLS");
        }
        else
            mb_areTimesValid = true;
    }
    const int TEMP_INTERVAL = mi_clockInterval;

    while ( mb_isClockActive )
    {
        cout << "// Interval Clock Selected \\" << endl << endl;

        if ( mi_clockHours == 0 && mi_clockMinutes == 0 && mi_clockSeconds == 0 )
            break;

        cout << mi_clockHours << ":" << mi_clockMinutes << ":" << mi_clockSeconds << flush;
        Sleep(1000);
        --mi_clockSeconds;

        // Handles minutes and seconds
        if ( mi_clockSeconds == 0 && mi_clockMinutes > 0 )
        {
            --mi_clockMinutes;
            mi_clockSeconds = 60;
        }

        // Handles minutes and hours
        if ( mi_clockMinutes == 0 && mi_clockHours > 0 )
        {
            --mi_clockHours;
            mi_clockMinutes = 59;

        }

        // Handles intervals
        if ( mi_clockInterval <= TEMP_INTERVAL )
        {
            --mi_clockInterval;
            if ( mi_clockInterval == 0 )
            {
                while ( mi_beap )
                {
                    cout << "\a \a \a" << flush;
                    Sleep(200);
                    --mi_beap;
                }
                mi_beap = 4;
                mi_clockInterval = TEMP_INTERVAL;
            }
        }

        if ( mi_clockSeconds < 0 )
            mi_clockSeconds = 60;

        system("CLS");
        if ( mi_clockHours == 0 && mi_clockMinutes == 0 && mi_clockSeconds == 0 )
            mb_isClockActive = false;
    }

    cout << "DONE" << flush;
    while ( mi_beap )
    {
        cout << "\a" << flush;
        Sleep(500);
        --mi_beap;
    }
}

C:
void StopWatchClock::printStopwatchClock()
{

    mi_clockHours = 0;
    mi_clockMinutes = 0;
    mi_clockSeconds = 0;

    while ( !GetAsyncKeyState(VK_ESCAPE) )
    {
        cout << "// Stopwatch Clock Selected \\" << endl << endl;
        cout << "P = Pause" << endl;
        cout << "ESC = Stop" << endl << endl;
        cout << mi_clockHours << ":" << mi_clockMinutes << ":" << mi_clockSeconds << flush;
        Sleep(1000);
        ++mi_clockSeconds;

        // Handles minutes and seconds
        if ( mi_clockSeconds == 60 )
        {
            ++mi_clockMinutes;
            mi_clockSeconds = 0;
        }

        if ( GetAsyncKeyState('P') )
            system("PAUSE");

        // Handles minutes and hours
        if ( mi_clockMinutes == 60 )
        {
            ++mi_clockHours;
            mi_clockMinutes = 0;

        }
        system("CLS");
    }
}

Main.cpp
C:
int main()
{
    cout << "// Select clock Type \\" << endl << endl;
    cout << "1: Normal Clock" << endl;
    cout << "2: Interval Clock" << endl;
    cout << "3: Stop Watch" << endl;
    int option;
    while ( cin >> option )
    {
        if ( option < 0 || option > 3 )
            cout << "Invalid Option" << endl;
        else
            break;
    }
    system("CLS");
    switch ( option )
    {
        case 1:
            CountdownClock countdown;
            countdown.printCountdownClock();
            break;
        case 2:
            IntervalClock interval;
            interval.printIntervalClock();
            break;
        case 3:
            StopWatchClock stop;
            stop.printStopwatchClock();
            break;
    }
}
 
Last edited:
AceInfinity

AceInfinity

Enthusiast
Messages
146
Reaction score
39
Points
85
Sin$
0
Unsafe use of std::cin here again (look up on input streams to see what I'm referring to). Also, just nitpicking but 'beap' is spelled 'beep'. You shouldn't be using system("CLS") though either -- there are better alternatives to clear the output buffer than using a system() call. Look into Win32 functions such as GetConsoleScreenBufferInfo, FillConsoleOutputCharacter, FillConsoleOutputAttribute, SetConsoleCursorPosition. Your switch statement should probably have a default case that handles invalid integer input too.
 
Top Bottom
Login
Register