Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A time counter in c++ [closed]

Tags:

c++

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

like image 211
cemil çelik Avatar asked Oct 11 '15 13:10

cemil çelik


1 Answers

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;
}
like image 78
Anwesha Avatar answered Oct 09 '22 16:10

Anwesha