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 );
CodePudding user response:
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
CodePudding user response:
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:
- std::setfill (https://en.cppreference.com/w/cpp/io/manip/setfill) sets the fill character
- std::setw (http://en.cppreference.com/w/cpp/io/manip/setw) sets the width of the box to fill.
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.
CodePudding user response:
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.
