Develop a program that simulates the operation of a digital stopwatch, which allows you to measure time intervals with millisecond accuracy. The time interval should be measured from the moment of launching the program to the moment of interactive interruption of its work on the signal from the keyboard. The change in time within the measured interval should be displayed in one line of the standard output stream (stdout). The output format should contain fields for displaying hours, minutes and seconds, separated by a colon, and a field for displaying milliseconds, which is separated from the seconds field by a period symbol. Updating information in the fields of the specified format should occur at intervals of one millisecond. In this case, only those digits of the output line whose values ​​have changed relative to the previously displayed time point should be modified.

1) How to make a modification of only a changed digit? And in general, the line as a whole (\ r is not an option, updates the entire line)
2) As in this case, make a stop by pressing the key (Not ctrl + C is desirable)

#include <stdio.h> #include <time.h> int main() { int start, end; int ms= 0; int ns= 0; int sec=0 , min=0 , hrs=0; start = clock (); while (1) { end = clock (); ns= end - start; ms = ns / 10; if (ms>100) { sec = sec + 1; ms= ms - 100; start= end; } if (sec > 59) { min = min+1; sec= 0; } if (min > 59) { hrs = hrs + 1; min=0; } printf ("%d:%d:%d.%d\n",hrs, min, sec, ms ); } } 
  • Create another thread and handle keystrokes in it. - xverizex

1 answer 1

There is an option - either use \r and output a new line on top of the old one (thus the numbers will look like not changed ...), or (since this option does not suit you)

use the "slaughter" for the desired number of characters and display only the changed numbers.

I would write everything in the string I stored. After that, I would compare two lines, and look where the difference begins.

With keys - if you have the same <conio.h> in the system, then check in the same cycle whether the key was pressed, and if it was, then what ...

  • I would run this code in a stream, but mostly expect getchar(); , with the passage of the character would kill the flow .. something like this - NewView
  • @NewView is also an option. - Harry
  • @Harry It is possible more specifically about what the "face" is, and how to implement it - Yaroslav Lapenkov
  • The backspace character. He \b . - Harry
  • @NewView is more specific, how can this be done? - Yaroslav Lapenkov