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 8411003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:19:40+00:00 2026-06-10T00:19:40+00:00

How would I effectively cancel a call for user input if there is no

  • 0

How would I effectively cancel a call for user input if there is no input within a certain time? (I’m programming a game for a terminal/cmd window with Mac OS X).

I have tried turning off canonical buffering and using a timer thread that joins after the call for user input. I also tried implementing the call to pthread_join() within the parameters of the while loop. Still nothing. The problem is that even though canonical buffering is off, the call for user input is still held up when there is no input. It works fine if there is input though.

It would be great if I could do this without fiddling around with downloading and installing ncurses, but I’ll do it if I have to.

Edit: Source code:

//Most headers only pertain to my main program. 
#include <iostream>
#include <termios.h>
#include <pthread.h>
#include <time.h>
#include <cstring>
#include <stdio.h>
#include <string.h>

using namespace std;

//Timer function.
void *Timer(void*) {

    time_t time1, time2;

    time1 = time(NULL);

    while (time2 - time1 < 1) {
        time2 = time(NULL);
    }

    pthread_exit(NULL);
}

int main() {

    //Remove canonical buffering.
    struct termios t_old, t_new;
    tcgetattr(STDIN_FILENO, &t_old);
    t_new = t_old;
    t_new.c_lflag &= ~ICANON;
    tcsetattr(STDIN_FILENO, TCSANOW, &t_new);

    cout << "Press any key to continue." << endl;

    string szInput;

    int control = 0;

    do {

        pthread_t inputTimer;

        pthread_create(&inputTimer, NULL, Timer, NULL);

        szInput = "";

        while (szInput == "") {

            szInput = cin.get();


            //Handle keypresses instantly.
            if (szInput == "a") {
                cout << endl << "Instant keypress." << endl;
            }
        }    

        pthread_join(inputTimer, NULL); 

        cout << endl << "One second interval." << endl;

        control ++;

    } while (control < 25);

    cout << "Game Over." << endl;

    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-06-10T00:19:41+00:00Added an answer on June 10, 2026 at 12:19 am

    One way of checking if there is input is to poll the file descriptor STDIN_FILENO using e.g. the select system call. If STDIN_FILENO is readable, then you can read at at least one character. You can also pass a timeout to the select call.

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

Sidebar

Related Questions

I have a 1d array containing Nd data, I would like to effectively traverse
How can I return what would effectively be a contiguous GROUP BY in MySQL.
I would like to create a class that effectively does this (mixing a little
How would one go about tinting a Bitmap to a solid color, effectively replacing
Is it at all possible to create a member which would effectively be inaccessible
Does C# provide an effective means of scanning the available COM ports? I would
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would be possible to extend this kind of layout so that I can have
Would it be possible to put install THG on a shared drive and let
Would it be wiser to check if a particular element exists on the 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.