not able to work for alarm clock

This is a discussion on not able to work for alarm clock within the C Programming forums, part of the General Programming Boards category; i have want to simple program which will do the following-> 1. Display enter the name message 2. Accept input ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    not able to work for alarm clock

    i have want to simple program which will do the following->
    1. Display enter the name message
    2. Accept input for 5 secs
    3. if user is successful to write message in 5 secs then he wins
    4. else looses.

    I want to do it with the use of alarm() system call on unix.

    Code:
    #include<stdio.h>
    #include<sys/signal.h>
    #include<unistd.h>
    
    char *name;
    
    void wakeup()
    {
    scanf("%s",name);
    }
    
    int main()
    {
    while(1)
    {
    printf("Enter the name :->");
    signal(SIGALRM,wakeup);
    alarm(5);
    pause();
    }
    return 0;
    }

    Problems with the code:
    1. It is not following the flow, seems that pause() has more priority. The message ""Enter the name :->", is displayed with delay.
    2. whenever control goes to wakeup, scanf blocks the control for infinite period. I mean unless I press ENTER, control wont do anything. So whole idea of terminating the input process after 5 secs becomes useless


    how to do it???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no standard kbdhit function. Most input functions are line buffered. That means you have to hit enter. It will wait until you do.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  3. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. ANN: The Fourth Contest: Alarm Clock, sign up here
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 59
    Last Post: 08-10-2002, 12:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21