cin while timer

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>
#include <windows.h>
using namespace std;

bool timer()
    {
    int a=5;
        while(a>=0)
        {
            cout<<a<<",";
            --a;
            Sleep(1000);
        }

    }

int main()
{
    int ans1,a;
    cout << "Test with variants.\nChoose a,b,c,or d as the answer by typing the coresponding letter.\n";
        Sleep(3000);
    cout <<"Get ready for the first question.You have 5 seconds to answer\n";
        Sleep(5000);
    cout<<"Question one:\n What's 48+52?\n";
        do
        {
            cout<<"\na=96\n";
            cout<<"b=100\n";
            cout<<"c=104\n";
            cout<<"d=98\n";

        }
        while(timer(),cin>>a);

            if(ans1=a)
            {
                cout<<"Correct,the answer is 96.\n";
            }
            else
            {
                cout<<"Wrong.The right answer was 96\n";
            }
}


I want to type a variant (a,b,c,d) until the timer reaches 0 but I cant type while the timer is counting,and when it reaches 0 the program closes
Line 5-15: timer() is defined as a bool function, but it never returns any value.

Line 33: Your timer() function is going to get called, but the result is going to be ignored. Not sure if that is what you intended by the use of the comma operator.

cin is not a timed operation. It is not going to run concurrently with your timer.

line 19: ans1 is an uninitialized variable.
Line 35: Two problems.
1) You're using the assignment operator (=), not the equality operator (==).
2) Even if you correctly used the equality operator, you're going to be testing against an uninitialized variable. The result of the if statement is undefined behavior.
Last edited on
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <windows.h>
using namespace std;

bool timer()
    {
    int a=5;
        while(a>=0)
        {
            cout<<a<<",";
            --a;
            Sleep(1000);
        }

    }

int main()
{
    int ans1,a;
    cout << "Test with variants.\nChoose a,b,c,or d as the answer by typing the coresponding letter.\n";
        Sleep(3000);
    cout <<"Get ready for the first question.You have 5 seconds to answer\n";
        Sleep(5000);
    cout<<"Question one:\n What's 48+52?\n";
        do
        {
            cout<<"\na=96\n";
            cout<<"b=100\n";
            cout<<"c=104\n";
            cout<<"d=98\n";

        }
        while(timer());
            {
                cin>>ans1;
            }
            if(ans1==a)
            {
                cout<<"Correct,the answer is 96.\n";
            }
            else
            {
                cout<<"Wrong.The right answer was 96\n";
            }
}


I still can't type "a" unless the timer ends...
Line 5-15: timer() is defined as a bool function, but it never returns any value.

What exactly should a function return?
Since timer is a bool function. It should return true or false.

Lines 25-33: Your loop is going to cout 4 times every time timer() returns true (assuming you fix timer() to return something).

Lines 34-36: The {} and indentation here are misleading. It gives the impression the lines belong as part of the while. They are not.

Line 37: You fixed the incorrect operator, but you still haven't fixed the comparison with an uninitialized variable (garbage).

As I pointed out before, cin is a waited operation. You can't time it. Once you call cin, your program won't continue until the user types something and presses enter.

You might consider using GetAsyncKeyState.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx
Last edited on
You can still have a timer going once cin is called with threads.
That's true, but that doesn't help the OP. The OP wants to cancel the cin when the timer expires.
Hello all,

Just a thought here. In main call timer as while(timer(ans1)).

Then in the timer function:
1
2
3
4
5
6
7
8
9
10
11
12
bool timer(int &answer)
{
    get system time start
    get input // cin>>answer;
    get system time end

    figure time difference
    
    if(difference <= 5) return true

    return false  //  not part of the if 
}


It is an idea. I have not worked on the code yet.

Back in main you could change the code this way:

1
2
3
4
5
6
7
8
9
10
11
        while(timer(ans1))  // no ; needed here. It end the while.
        {
             if(ans1==a)
            {
                cout<<"Correct,the answer is 96.\n";
            }
            else
            {
                cout<<"Wrong.The right answer was 96\n";
            }
        }


If the timer fails the while loop fails and the program ends. Or you could rework this part to deal with the return from timer so you could print something saying that the user took to much time.

For what its worth,

Andy
closed account (E0p9LyTq)
Using the Sleep() function as a timer is the opposite of what you should be doing in this situation.

You need to get and store the current time before you ask the user your timed question, and compare that to the newer time after answering the question.

C++11 introduced the <chrono> header for doing that.
http://www.cplusplus.com/reference/chrono/
Topic archived. No new replies allowed.