Countdown

Again for my Trivia, I'd like to create a countdown, so the player would have like 60 seconds to answer before the code continues...
I tried this http://www.cplusplus.com/forum/beginner/317/ but it doesn't work...Nor the first or the second one, unless I use a _sleep() before the if...
I just use a timer and then work with the WM_TIMER message. I set the timer to 1000 milliseconds and then decrease my count by 1 everytime checking for it reaching 0 to end.
Isn't WM_TIMER designed for WinApi?
I am using basic console here...
Somebody, please?
closed account (9wX36Up4)
U can use
1
2
3
4
5
for(i=60;i>=1;i-1)
{
    cout<<i;
    Sleep(1000);
}

For Sleep code u must use #include<windows.h>
I don't need the program to pause for 60 seconds...I need the player to be able to write down the answer in those seconds...
Anything, pl0x? xD
You're going to need to check how long the program has been open.
Using ctime I think this would work:
1
2
3
4
5
time_t programstart, timepassed;

programstart = time(0);
// Later in the program
timepassed = time(0) - programstart;


time(0) will give you how many seconds have passed since January 1, 1970.

With this you'd check if timepassed >= 60 to see if 60 seconds have passed. Although I doubt it's 100% accurate
Last edited on
@warnis: he / she wants to countdown program and not to see how many time elapsed. it's a program for a test (somekinda school's).

@op: maybe you should have a timer, but unfortunately, i didn't know the code. but it is obviously what you want...
The code could be modified to suit your timing needs if you put some tought into it.
The more obvious problem is that you would need threading for it to actually work as the code would freeze up as you retrieve the answer otherwise (assuming cin and the likes on a console).
It isn't a school project, it's a personal project xD

I modified and tried your code...But it isn't working:
1
2
3
4
5
6
7
8
9
10
11
12
while( i < BQuestions.size() - 2) {
                cout << BQuestions[i] << endl;
                timeStart = time(0);
                timePassed = time(0) - timeStart;
                for ( int j = 0; j < 5; j++ ) {
                    if(timePassed > 5) {
                    cout << "Time's up!" << endl;
                    }
                    j++;
                    }
                getline(cin, answer);
                lowerCase(answer);

nor
1
2
3
4
5
6
7
8
9
while( i < BQuestions.size() - 2) {
                cout << BQuestions[i] << endl;
                timeStart = time(0);
                timePassed = time(0) - timeStart;
                    if(timePassed > 5) {
                    cout << "Time's up!" << endl;
                    }
                getline(cin, answer);
                lowerCase(answer);


The WHILE loop isn't closed because the code continues :)
Last edited on
Some pseudo code of how you might do it
while question is incorrect and time is not up
  ask the question
  answer the question
  update time left
I don't believe this will work without the WinAPI, because (if memory serves) console programming does not use multiple threads. You could try Warnis's solution and check how long the program has been running by using the current time.
Tried that..Isn't working...I am thinking about moving it to a gaming platform...Either Allegro or Maybe DirectX xD
Topic archived. No new replies allowed.