<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: salieu Gbla</title>
    <description>The latest articles on DEV Community by salieu Gbla (@30gbla).</description>
    <link>https://dev.to/30gbla</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1250255%2F7fef73f5-5875-4b84-b1e3-1f68389e6682.jpeg</url>
      <title>DEV Community: salieu Gbla</title>
      <link>https://dev.to/30gbla</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/30gbla"/>
    <language>en</language>
    <item>
      <title>Create stopwatch with C++</title>
      <dc:creator>salieu Gbla</dc:creator>
      <pubDate>Sat, 06 Jan 2024 12:00:33 +0000</pubDate>
      <link>https://dev.to/30gbla/create-stopwatch-with-c-5g9g</link>
      <guid>https://dev.to/30gbla/create-stopwatch-with-c-5g9g</guid>
      <description>&lt;p&gt;cpp&lt;/p&gt;

&lt;p&gt;beginners&lt;/p&gt;

&lt;p&gt;tutorial&lt;/p&gt;

&lt;p&gt;showdev&lt;br&gt;
C++ is a great language but to make something using it is a bit difficult. But that doesn't mean you can't do anything with it. You can create small console based applications and that's what we are going to do. I will show you how to create a simple stopwatch with C++.&lt;/p&gt;

&lt;p&gt;I got this idea from my javascript stopwatch project and thought how can I create something like that in C++. So let's see how to do it !!&lt;/p&gt;

&lt;p&gt;Requirements&lt;br&gt;
C++ compiler (or you can use any online platform. My recommendation is: Repl.it)&lt;br&gt;
Text editor&lt;br&gt;
Some knowledge of C++&lt;br&gt;
Step 1: Write the base code&lt;br&gt;
We will start by writing the base code for our console application.&lt;/p&gt;

&lt;p&gt;Create a folder and give it a name. e.g. cpp-stopwatch&lt;br&gt;
Create a file named stopwatch.cpp in that folder.&lt;br&gt;
// stopwatch.cpp&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
   cout &amp;lt;&amp;lt; "stopwatch" &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;br&gt;
To compile and run this code, open your terminal or command prompt and navigate to that folder and run the following commands&lt;/p&gt;

&lt;h1&gt;
  
  
  compile
&lt;/h1&gt;

&lt;p&gt;g++ stopwatch.cpp -o stopwatch&lt;/p&gt;

&lt;h1&gt;
  
  
  run
&lt;/h1&gt;

&lt;p&gt;./stopwatch&lt;br&gt;
Use these commands every time you make changes to your C++ file.&lt;/p&gt;

&lt;p&gt;Step 2: Displaying our stopwatch&lt;br&gt;
First we will start by displaying our stopwatch in console before adding functionality to it. For that we will create necessary variables and a function named displayTime to display our stopwatch.&lt;br&gt;
// stopwatch.cpp&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;void displayTime(int hours, int minutes, int seconds) {&lt;br&gt;
   cout &amp;lt;&amp;lt; hours &amp;lt;&amp;lt; ":"&lt;br&gt;
        &amp;lt;&amp;lt; minutes &amp;lt;&amp;lt; ":"&lt;br&gt;
        &amp;lt;&amp;lt; seconds &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
   int hour = 0;&lt;br&gt;
   int min = 0;&lt;br&gt;
   int sec = 0;&lt;/p&gt;

&lt;p&gt;displayTime(hour, min, sec);&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
Step 3: Stopwatch functionality !!&lt;br&gt;
Now the interesting part, functioning of stopwatch. If you are familiar with javascript, you already know that there is a built-in function setInterval that runs the code after the specified interval. But how can we do that thing in C++ ??&lt;/p&gt;

&lt;p&gt;In C++ we can use a function called sleep which is available in the header unistd.h in linux and windows.h in windows.&lt;/p&gt;

&lt;p&gt;Sleep function takes number of seconds and stops the code for the specified duration.&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include  // in linux: sleep()
&lt;/h1&gt;

&lt;p&gt;// #include  // in windows: Sleep()&lt;/p&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
   cout &amp;lt;&amp;lt; "wait for 5 seconds !" &amp;lt;&amp;lt; endl;&lt;br&gt;
   sleep(5);&lt;br&gt;
   cout &amp;lt;&amp;lt; "5 seconds passed away !" &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;br&gt;
So, we have a way to stop our code but we need a way to increment number of seconds continuously. For that we will use LOOOOOP.&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include  // in linux: sleep()
&lt;/h1&gt;

&lt;p&gt;// #include  // in windows: Sleep()&lt;/p&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
   int hour = 0;&lt;br&gt;
   int min = 0;&lt;br&gt;
   int sec = 0;&lt;/p&gt;

&lt;p&gt;// run the stopwatch continuously&lt;br&gt;
   while(true) {&lt;br&gt;
      sleep(1);&lt;br&gt;
      sec++;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  cout &amp;lt;&amp;lt; sec &amp;lt;&amp;lt; endl;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;&lt;br&gt;
}&lt;br&gt;
Let's add the code for hours and minutes as well and also use our display function&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include  // in linux: sleep()
&lt;/h1&gt;

&lt;p&gt;// #include  // in windows: Sleep()&lt;/p&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;void displayTime(int hours, int minutes, int seconds) {&lt;br&gt;
   cout &amp;lt;&amp;lt; hours &amp;lt;&amp;lt; ":"&lt;br&gt;
        &amp;lt;&amp;lt; minutes &amp;lt;&amp;lt; ":"&lt;br&gt;
        &amp;lt;&amp;lt; seconds &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
  int hour = 0;&lt;br&gt;
  int min = 0;&lt;br&gt;
  int sec = 0;&lt;/p&gt;

&lt;p&gt;while(true) {&lt;br&gt;
    sleep(1);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sec++;

if(sec &amp;gt; 59) {
  min++;
  sec = 0;
} 

if(min &amp;gt; 59) {
  hour++;
  sec = 0;
  min = 0;
}

displayTime(hour, min, sec);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
}&lt;br&gt;
Running this code will start printing stopwatch with new time, line by line. We can handle this by simple clearing screen before displaying the next time.&lt;/p&gt;

&lt;p&gt;Add system("clear") in displayTime function&lt;br&gt;
We will also display initial time before entering the loop so that stopwatch display time from 0 hours, 0 minutes and 0 seconds instead of directly showing 1 second in console&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include  // in linux: sleep()
&lt;/h1&gt;

&lt;p&gt;// #include  // in windows: Sleep()&lt;/p&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;void displayTime(int hours, int minutes, int seconds) {&lt;br&gt;
   // for linux&lt;br&gt;
   system("clear"); &lt;/p&gt;

&lt;p&gt;// for windows&lt;br&gt;
   // system("cls");&lt;/p&gt;

&lt;p&gt;cout &amp;lt;&amp;lt; hours &amp;lt;&amp;lt; ":"&lt;br&gt;
        &amp;lt;&amp;lt; minutes &amp;lt;&amp;lt; ":"&lt;br&gt;
        &amp;lt;&amp;lt; seconds &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
  int hour = 0;&lt;br&gt;
  int min = 0;&lt;br&gt;
  int sec = 0;&lt;/p&gt;

&lt;p&gt;displayTime(hour, min, sec);&lt;/p&gt;

&lt;p&gt;while(true) {&lt;br&gt;
    sleep(1);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sec++;

if(sec &amp;gt; 59) {
  min++;
  sec = 0;
} 

if(min &amp;gt; 59) {
  hour++;
  sec = 0;
  min = 0;
}

displayTime(hour, min, sec);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
}&lt;br&gt;
Finally, compile and run it and hopefully you will see your very own console stopwatch. To stop the stopwatch, use Ctrl + C&lt;/p&gt;

&lt;p&gt;This one was beginner friendly and doesn't use threads and chrono library. You can find the source code and working example in my repl.&lt;/p&gt;

&lt;p&gt;I am not an expert in C++ but trying new things and applying them is important. Hope you learned something new out of this simple C++ stopwatch project.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
