Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we get the time between two inputs?

Tags:

c++

time

input

cin

I was wondering while watching this code :

#include<iostream>
#include<conio.h>

using namespace std;

int main(){

    int a,b;

    cout << "enter 1";
    cin  >> a;

    cout << "enter 2";
     cin >> b;

    getch();
    return 0;
}  

if we can get the time gap between the input of variables a and b successively.

like image 753
user3284775 Avatar asked Feb 21 '26 23:02

user3284775


1 Answers

Use time() to get current time and difftime() to compute the difference.

#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;
int main()
{
    int a,b;
    cout<<"enter 1";
    cin>>a;
    time_t atime = time(NULL);
    cout<<"enter 2";
    cin>>b;
    time_t btime = time(NULL);
    cout << difftime(btime, atime) << " seconds passed\n";
    getch();
    return 0;
}  
like image 167
timrau Avatar answered Feb 24 '26 14:02

timrau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!