0

I am a beginning programmer, and am trying to work with exception handling in C++. I found a method for validating input that seems pretty versatile, but I want to add an exception that will end the program if a user enters Ctrl-z.

Here is the function I found:

cout << "Please enter a decimal value:" << endl;
        while ((cin >> number).fail() || number < 0 || cin.peek() != '\n'){
            cin.clear();
            while (cin.get() != '\n'){ continue; cin >> number; } cout << "Invalid Input" << endl;

I am working on a problem with vectors. I know that Ctrl-z sends an empty string; I'm wondering if there is a way to incorporate a condition that breaks from here if number is equal to Ctrl-z?

I hope that this question is clear enough.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
user3842633
  • 37
  • 1
  • 3
  • 2
    Please see [this related question](http://stackoverflow.com/questions/17766550/ctrl-c-interrupt-event-handling-in-linux) – user703016 Sep 05 '14 at 18:30
  • 1
    @dolan's right! You can install signal handlers that in turn throw exceptions. But be careful, when using this technique with multithreaded programs. CTRL-Z actually closes `std::cin` and no there's no number you can use to pass it via `std::cin`. – πάντα ῥεῖ Sep 05 '14 at 18:33
  • Control-z in a Unix environment is associated with the SIGTSTOP signal, so keep that in mind when considering the related question @dolan found (which deals with the SIGINT signal). – Jim Lewis Sep 05 '14 at 18:37

0 Answers0