0

I'm trying to program a digital clock with an input value and the seconds need to be updated every time we press the ENTER key until 5 seconds have been passed. At the same time, when the input value is 23:5:43, the program automatically needs to convert that time to 23:05:44. How should I resolve this problem because the time is being updated without me pressing any key. I need to use the do-while loop for this exercise.


#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    int hh=00, mm=00, ss=00, lines = 0;
    char s;

    cin >> hh >> s >> mm >> s >> ss;
    do 
    {
        ss++;
        sleep(1);

        if (ss == 60)
        {
            ss = 0;
            mm++;
            cout << hh << ":" << mm << ":" << ss << endl;
            lines++;
            
            if (mm == 60)
            {
                mm = 0;
                hh++;
                cout << hh << ":" << mm << ":" << ss << endl;
                lines++;

                if (hh == 24)
                {
                    hh = 0;
                    ss = 0;
                    mm = 0;
                    cout << hh << ":" << mm << ":" << ss << endl;
                    lines++;
                }
            }
        }
            

        if (hh < 10)
        {
            cout << 0;
            cout << hh << ":";

            if (mm < 10)
            {
                cout << 0;
                cout << mm << ":";

                if (ss < 10)
                {
                    cout << 0;
                    cout << ss << endl;
                }
            }
        }
        else {
            cout << hh << ":" << mm << ":" << ss << endl;
            lines++;
        }


    } while( lines >= 5 );
  • The problem of displaying `05` instead of `5` should really be a separate question. It will likely be closed as a duplicate, or just get comment telling you to get [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) (because it's really a basic beginners issue that should have been taught by most decent beginners books). – Some programmer dude Jan 24 '22 at 11:47

3 Answers3

0

2 mistakes if you want to stop after 5 seconds: Sleep(1) sleeps for 1 millisecond and you increase lines every minute (now 60ms) instead of every second (you also increase it again every hour and every day and every loop if the hour is at least 10, which is also wrong).

For formatting your output have fun with this documentation: https://en.cppreference.com/w/cpp/io/manip

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Oops, actually I meant sleep(1) with 's' in small letter. So to put an example, if my input is 23:04:05, the output should be 23:04:06, 23:04:07, 23:04:08, 23:04:09, 23:04:10 and the program should stop. That's why I'm incrementing lines to count how many lines are displayed. But most probably something is wrong. Just wanted to make my doubt clear. – Ruth Mary Thannikuzhuppil Jan 24 '22 at 12:16
  • Yes, just do `lines++` once at the end of the do-while-loop – stefaanv Jan 24 '22 at 14:41
0

May I guide you to some std-library functions?

std::this_thread::sleep_for for example (https://en.cppreference.com/w/cpp/thread/sleep_for). This uses a std::chrono::duration as parameter, so you are not hitting the problem of accidentally waiting for ms instead of seconds:

#include <chrono>
#include <thread>
 
void foo()
{
    using namespace std::chrono_literals;
    std::this_thread::sleep_for(2000ms);
}

Your second issue is with formatting output. The iostream manipulators can really help here:

So for your example, this will be something like this:

std::cout << std::setfill(0) 
          << std::setw(2) << hour << ":"
          << std::setw(2) << minute << ":"
          << std::setw(2) << seconds << "\n";

Your sample code contains loads of code duplication; this is something to get rid of (When fixing issues, you don't want to fix it more than once).

And, there is clearly an error: if you pass a full minute within your five lines, you will print the time twice, three times for a full hour and even four times, if you have a day-change.

0

The descriptions in the title and the body are not the same. Anyway, if you want to update the time every second but also every time Enter is pressed, you need a keyboard input function with a timeout value. Such a function will be named keypress or something (you tell us nothing of the environment).

Before calling keypress, compute the delay between the current time and the next second, and use this value as the timeout. Whenever keypress exits, update the time and loop.

If your application needs to run for a longer time, do not rely on yourself accumulating the seconds or fractions of seconds to keep the time updated: read the system clock instead.

  • 5 seconds is probably too short a delay to allow yo to test test the program comfortably. –  Jan 24 '22 at 14:29