我们可以在两个输入之间获得时间吗?

use*_*775 3 c++ time input cin

我在看这段代码时想知道:

#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;
}  
Run Code Online (Sandbox Code Playgroud)

如果我们可以连续得到变量a和b的输入之间的时间差.

tim*_*rau 6

使用time()来获得当前的时间和difftime()计算的差异.

#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;
}  
Run Code Online (Sandbox Code Playgroud)

  • @OrelEraki我不同意.OP要求输入变量a和b_之间的时间间隔.如果`atime`早于一行提取,则等待'a`的时间被计入. (3认同)