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>
usingnamespace 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;
}
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.
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>
usingnamespace 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!