Is there a way to know how long it takes user to input?

Jun 19, 2015 at 5:02pm
I am writing a math quiz game for my brother. And I want to know on which questions he takes more time and on which questions he takes lesser times to think.
So is there a way to know long long it takes him to input the answer?

Thanks in advance!
Last edited on Jun 19, 2015 at 5:03pm
Jun 19, 2015 at 5:27pm
Call time() before and after the cin statement.
Use difftime to compute the elapsed time.
http://www.cplusplus.com/reference/ctime/time/
http://www.cplusplus.com/reference/ctime/difftime/
Jun 19, 2015 at 5:30pm
You may use f.e. the time() function from <time.h>. It returns the time in seconds since beginning of 1970. Get the time when initially displaying your math question and another time when your brother answers it. The difference of both is the time in seconds he needs to answer the question.
Jun 19, 2015 at 9:19pm
Or you could use clock() in a similar way.
http://www.cplusplus.com/reference/ctime/clock/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <ctime>
using namespace std;

int main() {
    int answer = 0;

    clock_t start = 0;
    clock_t stop  = 0;

    cout << "What is 2 + 2 ? ";

    start = clock();

    cin >> answer; // simple input

    stop = clock();

    cout << "\n";
    if(answer == 4)
        cout << "right! :-)\n";
    else
        cout << "wrong! :-(\n";
    cout << "\n";
    cerr << "It took you " << (static_cast<double>(stop - start)/CLOCKS_PER_SEC)
         << " secs to calculate it!\n";

    return 0;
}


What is 2 + 2 ? 5

wrong! :-(

It took you 4.672 secs to calculate it!


Andy
Last edited on Jun 19, 2015 at 9:20pm
Jun 20, 2015 at 5:12pm
Thank you everyone for the help.
I got it to work exactly the way I wanted. Thanks a lot!

Andy, I didn't know about the clock() function. Thank you very much :)
Topic archived. No new replies allowed.