Exercise: Making a Stopwatch
Let's create a program that prints time in minutes, seconds, and centiseconds on the console.
We'll cover the following
Stopwatch
Write a code to create a stopwatch program, which displays time in minutes, seconds, and centiseconds (one-hundredth of a second).
Understanding the logic
We know that:
- 100 centiseconds = 1 second
- 60 second = 1 minute
- 60 minutes = 1 hour
We have provided you with a playground below to run and write your code. First, click “Run” to see the output of the code. Then read the explanation below it to understand the given code. Then add the missing code (to print seconds and minutes) in the same playground.
#include <stdlib.h> #include <unistd.h> #include <iomanip> #include <iostream> using namespace std; void timer(int min, int sec, int centisec); void display(int min, int sec, int centisec, bool& colon); void timer(int min, int sec, int centisec) { bool colon = true; while(true) { display( min, sec, centisec, colon); usleep(10000); centisec++; // Write your code for seconds and minutes } } void display(int min, int sec, int centisec, bool& colon) { cout << "\r"<<setfill('0') << setw(2) << min; if(colon) cout << " : "; else cout << " "; cout << setfill('0') << setw(2) << sec; if(colon) cout << " : "; else cout << " "; cout << setfill('0') << setw(2) << centisec << flush; colon = !colon; } int main() { int min = 0, sec = 0, centisec = 0; timer(min, sec, centisec); return 0; }
If we run the above program, we see a stopwatch that shows the time that elapses only in centiseconds.
-
In line 3, we’ve included a library named
<iomanip>
. In this library,setw
andsetfill
are defined, which are used in thedisplay()
function. -
In line 15, we’ve used the
usleep()
function to add delay. This is a built-in function that takes a number of microseconds as a parameter. For example, we can inputusleep(10000)
inside the display. Adjust the integer value and see which value is closer to a centisecond. -
In lines 22, 27, and 32, the
setw
function is used to set the width of the output operation. As we can see,setw()
with width 2 is used for minutes because minutes are not greater than 59. Any number between 10–59 is 2 digits; that’s why the field is set to 2. Note that for the numbers 0–9, we print 00, 01, …, 09. -
In lines 22, 27, and 32, the
setfill()
function is used to initialize the set width character to whatever we like. For example, if we setsetfill('0')
and we print a string/number with width 5 and the actual string or number we print is of 3 lengths then the remaining width will be filled with'0'
.The syntax for
setw
andsetfill
is:
setw(width);setfill('0'); // '0' can be replaced with any other character
We can also use \n
\r
\t
in the code:
-
\n
means newline. It is a short form ofendl
except\n
is written inside double-quotes. -
\t
is used for the horizontal tab and it means several spaces (a total of eight) are applied on output. -
\r
is used for carriage return. It tells the compiler to move the cursor to the start of the line. In printing the time, we must print\r
first. This results in the cursor moving to the beginning of the line again and again, thus enabling overwriting and giving the effect of the timer moving forward.
Exercise: The timer()
function
Now we can write the code in the editor above to display time in seconds and minutes as well. Click the terminal below to see how the expected output should look like.
Direction
We should add the condition that if the centiSec
value becomes 100, it should increment a value in the sec
variable. Then, when the sec
value becomes 60, then the min
should get incremented by 1. Note that for every sec
increment, the centiSec
must be reset. Similarly, in the case of min
increment, the sec
value should reset.
Write the code inside the timer()
function in the playground above.
Once you’ve written the code, click the “Show Solution” button to see the solution.