How to check in background whether a minute has passed while execution of the main program in c++?

258 Views Asked by At

I have created a program which displays the current time on screen. What I want to do now is to update that time after every minute.

dateAndTime now; //created an object of a custom class dateAndTime

cout << "\n\t\t\t\t\t\t" << now.hour() << ':' << now.minute() << endl;
cout << "CASH MANAGER" << "\n";
cout << "1. Add cash" << "\n";
cout << "2. Withdraw cash" << "\n\n";

int ch;
cout << "Enter a number 1 or 2: ";
cin >> ch;

Let's assume the time is updated through the following process:

if (time(0) == now.epochTime() + 1)
    now.setTime(time(0));

Now, what I want to do is that the time displayed as now.hour() << ':' << now.minute(); should update after every minute without delaying or interrupting the main program.

That means if the user for example doesn't provide any input for several minutes after the prompt, the time should automatically be updated after each minute and throughout the process the program is always waiting for the user's input (i.e the main program is running as usual). Is there any way of doing so? I know that there might not be a cross-platform technique for doing this. So I just want to do this for windows os.

EDIT: restoring current cursor position or any written text by the user (which has not been entered yet) is not required after the update.

1

There are 1 best solutions below

0
On

you can use the alarm() function, which will, at the expiration of the time, sends a signal SIGALRM.

The application just needs to have a signal handler for that signal.