Timer

I would like to put a timer for my program..
I'm making a program that is somehow like a quizbee..
I would like to allot 10 seconds for the questions..when the time reaches 0..i would like the program to post the message "time is up!" then terminate...how would i be able to do that??so far here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char ans;
cout<<"what is the library for getch()?\n";
cout<<"a.#include<iostream> b.#include<conio.h> c.#include<ctime>\n";
cout<<"answer:";
cin>>ans;
if(ans=='b'||ans=='B')
cout<<"correct!";
else
cout<<"incorrect!";
getch();
return 0;
}


Thank you!
Last edited on
You could use sleep(): void sleep(unsigned seconds), just remember that it's not standard and depends on your compiler
matsom could you please post a sample code for that so that i would be able to understand it further...thank you very much for your response
ok. http://www.dreamincode.net/forums/topic/16391-sleep-command-in-c/
pay attention to the whole discussion
What if the user wants to enter value before 10 seconds?

What if the user wants to enter value before 10 seconds?

what do you mean?
the user has to put the answer within 10 seconds :)...so that means reading the question would be included in the time alloted

@matsom ok. i would! thank you very much!

What if the user wants to enter value before 10 seconds?


raprap, I think the reason oldnewbie put this is that the sleep command apparently "suspends program execution for a specified time interval". I belive this means that the program will not execute during this time.

If you want to keep the program functioning for 10s while the user can enter input, you might want to try C time functions. The time function will return a time value to you and the difftime function will tell you the difference in seconds between two times.

All of these functions are in the <ctime> header. You will need to have a vague idea about pointers and be happy with using (but not necessarily defining) custom data structures.

Hope this helps.
Sleep would only suspend the program..yes that was what i have understood based on the link matsom gave me...i tried it and it somehow put a pause to my program then it displayed the question..but thanks anyway matsom :)

@XAnder i have heard of <ctime> but i haven't tried it...thank you very much Xander!!..now i have to study how <ctime> works......sorry....i'm new here and i'm just starting to learn C++..thank you very much everyone for your response!
what do you mean?
the user has to put the answer within 10 seconds :)...so that means reading the question would be included in the time alloted


Try to run below code if you didnt understand what I mean.In this case you are giving the user 10 seconds to think about the question and not allowing him to answer before the end of 10 seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int c;

cout<<"What is this? (hint:5) :";
_sleep(10000); //ten seconds
cin>>c;

if(c==5)
    cout<<"\nYou are a genius!";
else
    cout<<"\nI shouldnt have let it sleep too much.";


return 0;
}

ok..now i get it...thank you very much for that oldnewbie..sorry if i didn't get what you mean..that is because I haven't learned that Sleep function before..so now i know how it works..thank you!
You're welcome.
Topic archived. No new replies allowed.