1

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.

hnefatl
  • 5,700
  • 2
  • 25
  • 47
Mohammed Maaz
  • 385
  • 2
  • 10
  • You woudn't be able to do this using cin-based input. – SergeyA Dec 23 '16 at 21:14
  • let's say I take input through getche() function ( library function), then is it possible? – Mohammed Maaz Dec 23 '16 at 21:18
  • Nope. Still will block your program until the key is pressed. – SergeyA Dec 23 '16 at 21:21
  • So you are saying that c++ doesnot provide a way of threading processes? However what little research I did it seems it does! – Mohammed Maaz Dec 23 '16 at 21:23
  • I have never said so. You can thread alright. But it's not going to help you here. – SergeyA Dec 23 '16 at 21:28
  • 1
    Why not use alarm and signal-handling function? – Jean-Baptiste Yunès Dec 23 '16 at 21:33
  • 1
    @Jean-BaptisteYunès, the question is, how'd you modify the already printed time without disturbing current cursor location - because you need to preserve cursor location for user's input. – SergeyA Dec 23 '16 at 21:37
  • 1
    Yeah, that's the difficulty. It's 2 questions in one; the multithreading is the easy part.... – user Dec 23 '16 at 21:38
  • @SergeyA I don't want the cursor position to be restored after updation! Then will it possible then? – Mohammed Maaz Dec 23 '16 at 21:42
  • 2
    You just have to use terminal capabilities... There exists escape sequences that let you control cursor positioning, etc. There exists a lot of libraries that can help, `ncurses` is one of them. See http://stackoverflow.com/questions/138153/is-ncurses-available-for-windows for windows. – Jean-Baptiste Yunès Dec 23 '16 at 21:45

1 Answers1

1

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.

user3629249
  • 15,986
  • 1
  • 19
  • 17