// Example 13-7: Reading input until interrupted.
#include <csignal>
#include <iostream>
#include <string>

volatile std::sig_atomic_t interrupted;

// Signal handler sets a global flag.
extern "C" void sigint(int sig)
{
  interrupted = 1;
}

int main()
{
  //
  if (std::signal(SIGINT, sigint) == SIG_ERR)
    std::cerr << "Cannot set signal handler\n";
  else
  {
    unsigned long count = 0;           // count lines
    while(! interrupted)
    {
      std::cout << "> ";               // user prompt
      std::string s;
      if (! std::getline(std::cin, s))
        // EOF does not terminate the loop, only SIGINT
        std::cin.clear();
      ++count;
    }
    std::cout << "I counted " << count << " line(s).\n";
  }
}
