Stopwatch in C++We will learn how to make a timer in C++ in this tutorial. The stopwatch begins when any key on the keyboard is pressed, and it doesn't end until another key is pressed. Let's learn how to build a timer in C++. We'll utilize a function named _kbhit() in this program. _kbhit()It is a feature that checks if a key has been depressed or not. To use this function, conio.h must be included. Usually, it verifies if a key has been depressed. When a key is pushed, a value other than zero is returned. Example of a program using _kbhit function in C++: Output: You haven't pressed a key. ............................................ Process executed in 1.22 seconds Press any key to continue. We can also utilize the delay() method in our program of stopwatch in C++. This function, which is declared in the "dos.h" header file, is used to delay the execution of the program for a predetermined amount of milliseconds. Unsigned int milliseconds is the amount of milliseconds needed to hold the programme, while void is the return type for the method delay(), which returns nothing. Here, we make advantage of delay to clearly notice the second increment in the stopwatch. It waits 5 seconds if the delay is 5000 seconds. Program Breakdown We will include the entire necessary library in our program to build a stopwatch in C++ to avoid any type of error. In the above peace of code, we have implemented the basic look of our stopwatch having hours denoted by HH and minutes are denoted by MM and seconds as SS. In the above mentioned code in C++, the while lope while executed if the function kbhit is not true i.e. It is a feature that checks if a key has been depressed or not. To use this function, conio must be included. Usually, it verifies if a key has been depressed. When a key is pushed, a value other than zero is returned. getch () is short for get character, conio.h library has a predefined function named "it." In Turbo C or Turbo C++, we utilize getch to show the characters to the user (). Program for creating Stopwatch in C++Output: stopwatch HH : MM : SS 0 : 0 : 22 press any key to stop the time after pausing is 0 : 0 : 22 Try the below code if the one above does not work on you IDE: We also utilize the Sleep() method in our program. Sleep()This function holds the execution of the program for a specified amount of milliseconds. It is stated in the header file "Windows.h". After the program waits for 1 second, we increment the SS (seconds) variable. When the second variable reaches 60, we set it to zero and increment the MM (minute) variable. We reset the minute to zero when it reaches 60 and increase the HH (hour) variable. Program Breakdown We going to include the entire necessary header file specially the windows.h which holds the execution of the program for a specified amount of milliseconds. Here as we done in above explained code of stopwatch, we have implemented the basic look of our stopwatch having hours denoted by HH and minutes are denoted by MM and seconds as SS. In the above mentioned code in C++, the while lope while executed if the function kbhit is not true i.e. It is a feature that checks if a key has been depressed or not. To use this function, conio must be included. Usually, it verifies if a key has been depressed. When a key is pushed, a value other than zero is returned. Another Program for Stopwatch in C++Output: stopwatch HH: MM: SS 0 : 0 : 0 press any key to start When a key is pressed, a timer begins, and after 29 seconds, the following output is displayed: stopwatch HH: MM: SS 0 : 0 : 29 press any key to stop And immediately after: stopwatch HH: MM: SS 0 : 0 : 30 press any key to stop Currently, pressing any key will halt the stopwatch and display the following output: stopwatch HH: MM: SS 0 : 0 : 30 press any key to stop the time after pausing is 0 : 0 : 30 Next TopicStudent Data Management in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India