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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. need a similar function as "getchar()"

need a similar function as "getchar()"

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 7 Posters 0 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.
  • B Offline
    B Offline
    bloodwinner
    wrote on last edited by
    #1

    if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C

    I R B S E 5 Replies Last reply
    0
    • B bloodwinner

      if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C

      I Offline
      I Offline
      InOut NET
      wrote on last edited by
      #2

      I don't think there is a function that 'does not wait for input'.

      B E 2 Replies Last reply
      0
      • I InOut NET

        I don't think there is a function that 'does not wait for input'.

        B Offline
        B Offline
        bloodwinner
        wrote on last edited by
        #3

        not necessarily a single function, but with the usage: keep program running, while accept the input if any

        C 1 Reply Last reply
        0
        • I InOut NET

          I don't think there is a function that 'does not wait for input'.

          E Offline
          E Offline
          eraccn
          wrote on last edited by
          #4

          I think what he wants is a kind of thread to wait for input.I suppose that you can use a thread to wait until he gets a string then uses thread communication to inform the main thread of this sting.

          1 Reply Last reply
          0
          • B bloodwinner

            not necessarily a single function, but with the usage: keep program running, while accept the input if any

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            What about multithreading? :)

            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.

            B 1 Reply Last reply
            0
            • B bloodwinner

              if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C

              R Offline
              R Offline
              Russell
              wrote on last edited by
              #6

              I think you can use feof (I think it works also on **stdin**) to know if the keyboard buffer is empty, elseway get the char with getchar. In this way you don't lock the program to wait a new key.


              Russell

              1 Reply Last reply
              0
              • C CPallini

                What about multithreading? :)

                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.

                B Offline
                B Offline
                bloodwinner
                wrote on last edited by
                #7

                I had been used multithread, and it works strange. It works fine when I run the program at foreground, the first thread keeps running while the second thread waiting for input. But it does not work well when I run it at background. Without any inputs, both the first and second threads will stop. Only after I manually switching it back to run at foreground, then the first thread runs again. So I really want to find something similar and simpler than the mulithread way.

                1 Reply Last reply
                0
                • B bloodwinner

                  if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C

                  B Offline
                  B Offline
                  bloodwinner
                  wrote on last edited by
                  #8

                  http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=2124286&df=100&fr=151

                  1 Reply Last reply
                  0
                  • B bloodwinner

                    if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C

                    S Offline
                    S Offline
                    Sam_c
                    wrote on last edited by
                    #9

                    my quick input before lunch if(kbhit()) { //get with getchar() } continue code... kbhit() will solve the problem off the top of my head, cant remember if its part of stdio or another header file... will look after lunch for you :) modified: its in conio.h _kbhit returns 0 if no key has been pressed, otherwise a nonzero int if has. -- modified at 6:27 Wednesday 11th July, 2007

                    1 Reply Last reply
                    0
                    • B bloodwinner

                      if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C

                      E Offline
                      E Offline
                      Emmanouil
                      wrote on last edited by
                      #10

                      An alternative way is to use the select() call (only if you use linux/unix platform). select will wait for a file descriptor to become active and read the data from this file descriptor. I assume you can establish a pipe interface with STDIN and then use select to monitor your fd. you can put select in a while loop to monitor the fds continuously and maybe fork() this function if you want control back...

                      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