DEV Community

Cover image for Create stopwatch with C++
Paras 🧙‍♂️
Paras 🧙‍♂️

Posted on • Edited on

Create stopwatch with C++

This is going to be a mini project in c++

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++.

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 !!

Requirements

  • C++ compiler (or you can use any online platform. My recommendation is: Repl.it)
  • Text editor
  • Some knowledge of C++

Step 1: Write the base code

We will start by writing the base code for our console application.

  • Create a folder and give it a name. e.g. cpp-stopwatch
  • Create a file named stopwatch.cpp in that folder.
// stopwatch.cpp
#include <iostream>

using namespace std;

int main() {
   cout << "stopwatch" << endl;
}
Enter fullscreen mode Exit fullscreen mode
  • To compile and run this code, open your terminal or command prompt and navigate to that folder and run the following commands
# compile
g++ stopwatch.cpp -o stopwatch

# run 
./stopwatch
Enter fullscreen mode Exit fullscreen mode

Use these commands every time you make changes to your C++ file.

Step 2: Displaying our stopwatch

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.

// stopwatch.cpp
#include <iostream>

using namespace std;

void displayTime(int hours, int minutes, int seconds) {
   cout << hours << ":"
        << minutes << ":"
        << seconds << endl;
}

int main() {
   int hour = 0;
   int min = 0;
   int sec = 0;

   displayTime(hour, min, sec);

}
Enter fullscreen mode Exit fullscreen mode

Step 3: Stopwatch functionality !!

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++ ??

In C++ we can use a function called sleep which is available in the header unistd.h in linux and windows.h in windows.

Sleep function takes number of seconds and stops the code for the specified duration.

#include <iostream>
#include <unistd.h> // in linux: sleep()

// #include <windows.h> // in windows: Sleep()

using namespace std;

int main() {
   cout << "wait for 5 seconds !" << endl;
   sleep(5);
   cout << "5 seconds passed away !" << endl;
}
Enter fullscreen mode Exit fullscreen mode

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.

#include <iostream>
#include <unistd.h> // in linux: sleep()

// #include <windows.h> // in windows: Sleep()


using namespace std;

int main() {
   int hour = 0;
   int min = 0;
   int sec = 0;

   // run the stopwatch continuously
   while(true) {
      sleep(1);
      sec++;

      cout << sec << endl;
   }  
}
Enter fullscreen mode Exit fullscreen mode

Let's add the code for hours and minutes as well and also use our display function

#include <iostream>
#include <unistd.h> // in linux: sleep()

// #include <windows.h> // in windows: Sleep()


using namespace std;


void displayTime(int hours, int minutes, int seconds) {
   cout << hours << ":"
        << minutes << ":"
        << seconds << endl;
}

int main() {
  int hour = 0;
  int min = 0;
  int sec = 0;

  while(true) {
    sleep(1);

    sec++;

    if(sec > 59) {
      min++;
      sec = 0;
    } 

    if(min > 59) {
      hour++;
      sec = 0;
      min = 0;
    }

    displayTime(hour, min, sec);
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

  • Add system("clear") in displayTime function
  • 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
#include <iostream>
#include <unistd.h> // in linux: sleep()

// #include <windows.h> // in windows: Sleep()


using namespace std;

void displayTime(int hours, int minutes, int seconds) {
   // for linux
   system("clear"); 

   // for windows
   // system("cls");

   cout << hours << ":"
        << minutes << ":"
        << seconds << endl;
}

int main() {
  int hour = 0;
  int min = 0;
  int sec = 0;

  displayTime(hour, min, sec);

  while(true) {
    sleep(1);

    sec++;

    if(sec > 59) {
      min++;
      sec = 0;
    } 

    if(min > 59) {
      hour++;
      sec = 0;
      min = 0;
    }

    displayTime(hour, min, sec);
  }
}
Enter fullscreen mode Exit fullscreen mode

Finally, compile and run it and hopefully you will see your very own console stopwatch. To stop the stopwatch, use Ctrl + C

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.

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.

Top comments (5)

Collapse
 
30gbla profile image
salieu Gbla

that impressive

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Thank you !

Collapse
 
30gbla profile image
salieu Gbla

impressive

Collapse
 
zinox9 profile image
Arjun Porwal

That's Awesome ! 🤘

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Thank youuu !!