timer for getline() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RajendraNPTL
    New Member
    • Mar 2013
    • 8

    timer for getline() function

    hiii all
    i want to read a string from commandline through getline() in c++. For that i want to add a timer of 5 sec. If no string read, then the program will terminate. can anybody know ?? I am using Centos 6.3
    thanks
  • suvarna271
    New Member
    • Feb 2013
    • 19

    #2
    /* Wait 5 seconds. */
    alarm(5);

    /* getline */

    /* Cancel alarm. */
    alarm(0);

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      What are you trying to do? Wait 5 seconds for user input and then go ahead?
      Last edited by weaknessforcats; Mar 20 '13, 06:45 PM. Reason: Banfa found an incorrect statement.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        getline can read from any input stream including cin

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You're right. I'll edit my post.

          Comment

          • RajendraNPTL
            New Member
            • Mar 2013
            • 8

            #6
            thnq suvarna.. it worked well.
            But, at last it is giving one message "Alarm clock". How to suppress that one ??? and also if i want to do another task after 5 sec with exiting from program.. is this one also possible ??
            -suvarna271

            Comment

            • RajendraNPTL
              New Member
              • Mar 2013
              • 8

              #7
              -weaknessforcats
              Exactly... but, not in wait. In that 5 sec time it needs to go to some other function and come back to the same getline(). Then with in 5 sec if i not entered any string, then again it has to got to that function and come back (looping)..
              Main purpose is__ Timer for getline(), with in that time it needs to execute another function and come back .. any ideas ??
              thanks

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                In that case you need to implement a multi-threaded program where your input is on a worker thread. That allows yur main thread to go elsewhere but also to return occasinally and inquire on the worker thread.

                Not a beginner solution but multithreading is very common.

                Comment

                • RajendraNPTL
                  New Member
                  • Mar 2013
                  • 8

                  #9
                  thank you -weaknessforcats
                  i already got the solution using <pthreads>
                  Now, i dnt want to use that. is there any other way to solve the same problem ..?? Challenging one... !!

                  thanks

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    I don't believe this can be accomplished within the Standard C Library, but perhaps your implementation provides the necessary functions...

                    Do you have an input function that returns immediately with a success/failure status rather than blocking until the input is obtained? If so, then you can write your main function to do its main job and only occassionally check for input.

                    Comment

                    • RajendraNPTL
                      New Member
                      • Mar 2013
                      • 8

                      #11
                      -donbock
                      ha ha.. main problem is that only.. if i have a proper function to return immediately, i never go for threads and alarm and timers all these. okay you came to my point. Now give your ideas about immediate returning(atlea st 2 sec) from getline() either with a success or failure .. ??
                      thanks

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        You will need to write your own input functions. Especially the one for the user input. All the standard functions wait indefinitely for the user to press the enter key. Your functon must only 2 secs for a keypress so you need to write your own input functions.

                        The standard input functions are just to get yoou started. Most applicatons of any size don't use them. Windows does not use them.

                        Comment

                        Working...