0

I want to make a program which shows how much time they took to insert a value. Like, program will ask the user to type their name and after they're done inputting their name the program will show how much they took to type it in seconds. I want to do it in OS specific ways

Famiu
  • 93
  • 7
  • What have you tried so far? Where have you looked? What search terms did you use? http://stackoverflow.com/help/how-to-ask – RJHunter Nov 04 '16 at 11:44
  • This is actually not a duplicate. The question addresses both user input and time. – YScharf Apr 05 '22 at 09:42

2 Answers2

1

You can do it using only the standard library, no OS specific code is required.

#include <iostream>
#include <string>
#include <chrono>

int main()
{
  using Clock = std::chrono::high_resolution_clock;

  std::cout << "Enter your name: ";
  std::string name;
  auto start = Clock::now();
  std::cin >> name;

  auto end = Clock::now();
  auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();

  std::cout << "Your input took " << ms << " milliseconds" << std::endl;
}

I used milliseconds, you can useother resolutions if you want.

MadScientist
  • 3,390
  • 15
  • 19
0

Using std::chrono :

#include <chrono>
using std::chorno; //make it less verbose...
.....
time_point<system_clock> start,end;
int diff_in_s=0;
start = system_clock::now();
your_user_input();
end = system_clock::now();
diff_in_s=duration_cast<seconds>(end-start).count();
user6556709
  • 1,272
  • 8
  • 13