Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6477129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:54:38+00:00 2026-05-25T06:54:38+00:00

I want to make a code in which Name of User will be asked

  • 0

I want to make a code in which Name of User will be asked to input, but in a time limit of 15 seconds. If user cross the limit & failed to input a name(or any string), then code will be terminated & “Time Out” Massage will be display otherwise Name should be saved & “Thanks” massage will be display. I had try like this but it’s wrong & not working. Please give me a solution for this.. Thanks.

#include <stdio.h>
#include <time.h>

int timeout ( int seconds )
{
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {}

    return  1;
}

int main ()
{
    char name[20];
    printf("Enter Username: (in 15 seconds)\n");
    printf("Time start now!!!\n");

    scanf("%s",name);
    if( timeout(5) == 1 ){
        printf("Time Out\n");
        return 0;
    }

    printf("Thnaks\n");
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:54:38+00:00Added an answer on May 25, 2026 at 6:54 am

    Probably this dummy program might help you:

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/time.h>
    #include <sys/types.h>
    
    #define WAIT 3
    
    int main ()
    {
        char            name[20] = {0}; // in case of single character input
        fd_set          input_set;
        struct timeval  timeout;
        int             ready_for_reading = 0;
        int             read_bytes = 0;
        
        /* Empty the FD Set */
        FD_ZERO(&input_set );
        /* Listen to the input descriptor */
        FD_SET(STDIN_FILENO, &input_set);
    
        /* Waiting for some seconds */
        timeout.tv_sec = WAIT;    // WAIT seconds
        timeout.tv_usec = 0;    // 0 milliseconds
    
        /* Invitation for the user to write something */
        printf("Enter Username: (in %d seconds)\n", WAIT);
        printf("Time start now!!!\n");
    
        /* Listening for input stream for any activity */
        ready_for_reading = select(1, &input_set, NULL, NULL, &timeout);
        /* Here, first parameter is number of FDs in the set, 
         * second is our FD set for reading,
         * third is the FD set in which any write activity needs to updated,
         * which is not required in this case. 
         * Fourth is timeout
         */
    
        if (ready_for_reading == -1) {
            /* Some error has occured in input */
            printf("Unable to read your input\n");
            return -1;
        } 
    
        if (ready_for_reading) {
            read_bytes = read(0, name, 19);
            if(name[read_bytes-1]=='\n'){
            --read_bytes;
            name[read_bytes]='\0';
            }
            if(read_bytes==0){
                printf("You just hit enter\n");
            } else {
                printf("Read, %d bytes from input : %s \n", read_bytes, name);
            }
        } else {
            printf(" %d Seconds are over - no data input \n", WAIT);
        }
    
        return 0;
    }
    

    Update:
    This is now tested code.

    Also, I have taken hints from man for select. This manual already contains a code snippet which is being used to read from the terminal and timeout in 5 seconds in case of no activity.

    Just a brief explanation in case the code is not well written enough:

    1. We add the input stream (fd = 1) to the FD set.
    2. We initiate select call to listen to this FD set created for
      any activity.
    3. In case any activity occurs within the timeout period, that is
      read through the read call.
    4. In case there was no activity, timeout occurs.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if its possible writing some code which will delete an
I want to make a code snippet database web application. Would the best way
Want to code a key pad for an calculator. What I want to make
I want to make sure the first 4 letters in a eight character code
For some MATLAB code that I want to make executable, I need the location
I have this code that I want to make point-free; (\k t -> chr
What considerations do I need to make if I want my code to run
I have style sheet with a class name changebackgroundcolor i want make change in
I want to make a query using which I can check that URL which
I want to make a language selection dropdown in a site user edit/create page.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.