7

I want to read a string from command line through getline() in c++.

For that I want to add a timer of 5 sec. If no string read, then the program will terminate.

How can I do this?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Reddi Rajendra P
  • 704
  • 9
  • 25
  • @RandyHoward : I don't think this is an exact duplicate. `getline()` is much simpler to use, and know how to add a counter to it would be useful to those who are new. This question should remain legitimate. – Games Brainiac Mar 20 '13 at 13:00
  • @kassak : Nope, still not an exact duplicate, the answers show that, I think. Either way, I think this is a useful question, because `getline()` is used quite a lot, and knowing how to use it well would be good for future readers. Just my 2 cents. – Games Brainiac Mar 20 '13 at 13:06
  • StackOverflow is not the proper place for this question. We do not write your code for you. You need to do your own coding and if you aren't sure why something is not working as expected, post the code with an explanation of what you were expecting it to do, and what it is actually doing including all error messages. See [about StackOverflow](http://stackoverflow.com/about). – Lightness Races in Orbit Mar 20 '13 at 13:09
  • 1
    @LightnessRacesinOrbit I would say the question is ok (after edit), since it has [quite good answer](http://stackoverflow.com/a/15524177/476681) – BЈовић Mar 20 '13 at 13:13
  • @BЈовић You would be wrong. – Lightness Races in Orbit Mar 20 '13 at 13:16
  • @GamesBrainiac no, this is not about using `getline` "well"; quite the opposite. If you need this kind of functionality, hacking it into getline is silly, because it will be a messy brittle hack. There are better ways of doing this, and they involve not using getline. – R. Martinho Fernandes Mar 20 '13 at 13:21
  • many ways..?? good.. what are the ways you have to solve this problem ?? - R. Martinho Fernandes – Reddi Rajendra P Mar 21 '13 at 05:43
  • @R.MartinhoFernandes : Why don't you elaborate on that, since you seem to know it quite well. – Games Brainiac Mar 21 '13 at 08:49
  • There is no standard-based solution to this, but there are libraries that can be used to do it, like ncurses. – R. Martinho Fernandes Mar 21 '13 at 09:49

2 Answers2

8

ok, wait for 5 sec, and terminate if there was no input:

#include <thread>
#include <atomic>
#include <iostream>
#include <string>

int main()
{
    std::atomic<bool> flag = false;
    std::thread([&]
    {
        std::this_thread::sleep_for(std::chrono::seconds(5));

        if (!flag)
            std::terminate();
    }).detach();

    std::string s;
    std::getline(std::cin, s);
    flag = true;
    std::cout << s << '\n';
}
Abyx
  • 12,345
  • 5
  • 44
  • 76
3

How about:

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

/* getline */

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

Alternatively you could use setitimer.


As R. Martinho Fernandes requested:

The function alarm arranges for the current process to receive a SIGALRM in 5 seconds from its call. The default action for a SIGALRM si to terminate the process abnormally. Calling alarm(0) disables the timer.

cnicutar
  • 178,505
  • 25
  • 365
  • 392