Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with Standard Input

Help with Standard Input

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danzy83
    wrote on last edited by
    #1

    Is there any function in C++ or C that checks if there is a character in the standard input and returns immediately? I have this application that creates a new thread when it starts working on an assigned task. The new thread asks the user to press some keys for other options as the main thread does its work. What I want is that if the main thread finishes doing its work and the user has not selected any of the options, then the thread that was created to get user options should return. But because the new thread waits for user input, I'm finding it difficult to stop it from waiting for user input so that it can return. I initially thought cin.peek() could do it but it also waits when the user has not press any key. I want a way to terminate the new thread as it waits for input, when the main thread has finished its work. Please help.

    CPalliniC C 2 Replies Last reply
    0
    • D Danzy83

      Is there any function in C++ or C that checks if there is a character in the standard input and returns immediately? I have this application that creates a new thread when it starts working on an assigned task. The new thread asks the user to press some keys for other options as the main thread does its work. What I want is that if the main thread finishes doing its work and the user has not selected any of the options, then the thread that was created to get user options should return. But because the new thread waits for user input, I'm finding it difficult to stop it from waiting for user input so that it can return. I initially thought cin.peek() could do it but it also waits when the user has not press any key. I want a way to terminate the new thread as it waits for input, when the main thread has finished its work. Please help.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      _kbhit [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • D Danzy83

        Is there any function in C++ or C that checks if there is a character in the standard input and returns immediately? I have this application that creates a new thread when it starts working on an assigned task. The new thread asks the user to press some keys for other options as the main thread does its work. What I want is that if the main thread finishes doing its work and the user has not selected any of the options, then the thread that was created to get user options should return. But because the new thread waits for user input, I'm finding it difficult to stop it from waiting for user input so that it can return. I initially thought cin.peek() could do it but it also waits when the user has not press any key. I want a way to terminate the new thread as it waits for input, when the main thread has finished its work. Please help.

        C Offline
        C Offline
        carter2000
        wrote on last edited by
        #3

        use _getch(), the header file is conio.h

        CPalliniC 1 Reply Last reply
        0
        • C carter2000

          use _getch(), the header file is conio.h

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          _getch() actually waits for the user input. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups