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
Asked
Active
Viewed 733 times
2 Answers
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