Timer in a C Program

S

sreelal

Dear All,

main()
{
int num;
printf("Enter a number ");
scanf("%d",&num);
}

In the above C program,
I would like the user to allow only 20 seconds to enter any number.
Just after 20 seconds, the program must terminate even if the user
has not entered any number. How can I set such a timer in the above
program. Also the decrementing time should be displayed on the screen,
along with other output,


Please suggest me a code in C to fullfill the above requirement.



Margee S
 
M

Malcolm

sreelal said:
main()
{
int num;
printf("Enter a number ");
scanf("%d",&num);
}

In the above C program,
I would like the user to allow only 20 seconds to enter any number.
Just after 20 seconds, the program must terminate even if the user
has not entered any number. How can I set such a timer in the above
program. Also the decrementing time should be displayed on the screen,
along with other output,


Please suggest me a code in C to fullfill the above requirement.
Unfortunately you are out of luck as far as ANSI C is concerned. stdin
(which scanf reads from) is a character stream usually created by the user's
keyboard. There is no way of manipulating it, for instance suppressing
output or polling for input.
However a very common extension is kbhit() (maybe called something else on
your system), which does check for input and returns if there is none. By
using a loop that calls time() and kbhit(), you can achieve what you want.
 
M

Materialised

sreelal said:
Dear All,

main()
{
int num;
printf("Enter a number ");
scanf("%d",&num);
}

In the above C program,
I would like the user to allow only 20 seconds to enter any number.
Just after 20 seconds, the program must terminate even if the user
has not entered any number. How can I set such a timer in the above
program. Also the decrementing time should be displayed on the screen,
along with other output,


Please suggest me a code in C to fullfill the above requirement.



Margee S
Assuming you are using a UNIX system which supports select() have a look
at the following code (Taken from Beejs Network programming tutorial).

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define STDIN 0 // file descriptor for standard input

int
main(void)
{
struct timeval tv;
fd_set readfds;

tv.tv_sec = 20;
tv.tv_usec = 0;

FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);

/* don 't care about writefds and exceptfds */
select(STDIN + 1, &readfds, NULL, NULL, &tv);


if (FD_ISSET(STDIN, &readfds))
printf("A key was pressed\n");
else
printf("Timed out.\n");

return 0;
}
 
A

Alberto =?iso-8859-1?Q?Gim=E9nez?=

El Tue, 14 Sep 2004 19:55:50 +0100, Materialised escribió:
Assuming you are using a UNIX system which supports select() have a look
at the following code (Taken from Beejs Network programming tutorial).

I think it's easier to use the alarm function (it is just for timeouts):

man alarm
 
R

Richard Bos

Alberto =?iso-8859-1?Q?Gim=E9nez?= said:
El Tue, 14 Sep 2004 19:55:50 +0100, Materialised escribió:

I think it's easier to use the alarm function (it is just for timeouts):

And I think differences of opinion like this are exactly why you should
take this to a system-specific group, where the people actually know the
system under discussion.

Richard
 
D

Dave

sreelal said:
Dear All,

main()
{
int num;
printf("Enter a number ");
scanf("%d",&num);
}

In the above C program,
I would like the user to allow only 20 seconds to enter any number.
Just after 20 seconds, the program must terminate even if the user
has not entered any number. How can I set such a timer in the above
program. Also the decrementing time should be displayed on the screen,
along with other output,


Please suggest me a code in C to fullfill the above requirement.



Margee S


Prior to input, fork a child which sleeps 20 seconds then kills its
parent. When you get the input, kill the child.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top