I want to programme a code in C++ that makes a time-counting at the same time while we are waiting the user to enter for example the integer result we want to see.For instance we want user to enter two integers and select one of the four operations,then write down the result.Meanwhile the clock or the counting machine starts to count the seconds until the user write down the result. Is it possible to do in C++,if it's not how can I do this? Thanks...
C++11/14 give you even more efficient ways than the old #include<ctime> headers of C
to measure time duration
using certain classes like steady_clock
, high_resolution_clock
, etc defined in the header #include<chrono>
. The following code does your job very efficiently:
#include <iostream>
#include <chrono>
using namespace std;
int main()
{
chrono::steady_clock sc; // create an object of `steady_clock` class
auto start = sc.now(); // start timer
// do stuff....
auto end = sc.now(); // end timer (starting & ending is done by measuring the time at the moment the process started & ended respectively)
auto time_span = static_cast<chrono::duration<double>>(end - start); // measure time span between start & end
cout<<"Operation took: "<<time_span.count()<<" seconds !!!";
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With