Reputation: 103
This will just display static time of the moment the function is called... How do you display running time?? Ie: when time changes from 12:00 to 12:01 it automatically shows on the screen I basically want to output a running clock at the top of my cmd screen and display other options etc underneath it
//http://stackoverflow.com/questions/997946/c-get-current-time-and-date
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://www.cplusplus.com/reference/clibrary/ctime/strftime/
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
Upvotes: 1
Views: 1637
Reputation: 477444
Take a deep breath, because this isn't going to be easy.
You will have to design your application fundamentally to multiplex several distinct, simultaneous "events", such as keyboard activity and timers.
You will also have to use an input/output method different from just reading from stdin and writing to stdout. You will need either a proper terminal, or a graphical interface.
Now, once you have got your head around those two ideas, you need to design your application. The fundamental ingredient is the event loop. This is an infinite loop that basically executes one round every time that at least one event is available. Depending on the event, you can take action: If the timer fires, update the time display. If the keyboard fires, process the input.
Whatever logic your program performs has to fit in there somewhere, too. For example, if the user input spells out a command that you recognize, you may start some action in response. Or you may have some action happen periodically on every 1000th firing of the timer. The result of one function may trigger an event which may itself be caught by the event loop. As long as your program logic executes fast enough to allow the timer to be handled in a timely fashion, this will be entirely acceptable. Only if your program has too much work can you start considering having multiple threads run the event loop concurrently (but that's a lot harder).
On Linux, the standard implementation for this design is epoll for the multiplexing and file-file descriptors, timerfds, eventfds and signalfds, and for the terminal control ncurses. Other platforms have corresponding technologies.
Upvotes: 1
Reputation: 409404
To start with you should look into the Windows console functions.
More specifically SetCursorPos
and WriteConsole
.
Upvotes: 0