Creating a Typing Speed Calculator in C++

In this article we’ll be demonstrating how to calculate a user’s typing speed by having them transcribe a sentence picked randomly from a list. This a fun little program to make that teaches novices how the “clock()” function from the “time.h” library works.

Getting The Libraries

Before getting started you should download this small list of sentences compiled here and place it in your main projects folder. Later on this list is used to generate random sentences for the user to type.

You’ll need to include the following five libraries in your program for it to run.

#include <iostream>  // Cout and Cin objects 
#include <fstream>  // Allows for file reading
#include <string>  // Allows usage of strings
#include <ctime>  // Allows for creating a timer using clock()
#include <stdlib.h>  // Allows for random number generation

Now with these libraries included you’re ready to define all the variables that you need.

Creating Variables

The first variables that you should declare are two strings, one for the randomly generated sentence and another for the user’s input.

std::string sentence;
std::string input;

Next you should create a clock_t variable called “t”. Clock_t is a fundamental arithmetic type that is capable of representing clock ticks, a unit of time which is constant but system specific. Ticks are what the “clock()” function uses to keep track of how long a program has been running for.

clock_t t;

You’ll need to generate a random number that’ll be used to grab a random line from the Sentences text file. The number generated should be somewhere between 0 and 55, the number of lines contained within the file.

srand((int)time(0));
int random = rand() % 55;

These are all of the variables that you’ll need for this program, now you can start generating random sentences.

Generating A Random Sentence

First you need to prompt the user to type out the following sentence, after that open up the text file using ifstream and define a string array with 55 entries in it.

std::cout << "Type the Following Sentence \n" ;
std::ifstream file("Sentences.txt");
std::string textarray[55];

Now use a for statement to iterate through the file and parse each line into the text array.

for(int i = 0; i<56; ++i)
{
     std::getline(file, textarray[i]);
}

Finally use the random number you generated earlier to assign “sentence” a random entry from the text array and print “sentence” to the screen.

sentence = textarray[random];
std::cout << sentence << '\n';

This will generate a new random sentence from the text file every time the program is run.

Calculating the User’s Typing Speed

Now you have to start a timer to calculate how long it takes the user to type in the sentence. To initiate the timer set the “t” variable from earlier equal to the clock function. This will begin counting how many ticks are passing by as the user types.

t = clock();

Next you should use std::getline to get the user’s input. Once they have finished entering a sentence initialize an int variable called endtime and set it equal to “t / CLOCKS_PER_SEC”. “CLOCKS_PER_SEC” is a variable containing how many clock ticks there are in a single second, dividing “t” by this number will produce roughly the amount of seconds the user took typing the sentence.

std::getline(std::cin, input);
int endtime = (clock() - t) / CLOCKS_PER_SEC;

After determining the user’s typing speed display it to the screen.

std::cout << "It took you " << endtime << " seconds to type out the sentence." << '\n';

You could just end the program here and be done with it if you want, but you may also want to show the user how many words per second they actually typed out. To do this we’ll be borrowing some code from the word counter C++ project that was covered in a past article. Use that code to determine how many words the user typed out and then divide endtime by words and that will give the user’s typing speed in words per second.

int words = 0;

for(int i = 0; input[i] !='\0'; i++)
{
    if (input[i] == ' ')
    {
      words ++;
    }
}
std::cout << "You have a typing speed of " << endtime/words << " words per second.";
return 0;

And now you have finished a basic typing speed calculator. It’s not the most accurate in the world for determining how lightning fast you are at typing but it’s still a nifty program nonetheless.

Further Suggestions

As it currently stands the program will tell you how fast you type but it doesn’t really reflect on how accurate what you typed out is. If you want an extra challenge try to add in a check at the end which determines how closely what the user typed out resembles the original string and then tell the user their accuracy rating.

Good Luck!

Download the above source code here.

If you have any comments or questions send them to me at nick@crumbsofcode.com