Here is the code:

#include "stdafx.h" #include <iostream> using namespace std; void pause() { cout << "Press ENTER key to continue or CTRL+C for exit ..." << endl; cin.get(); cin.ignore(cin.rdbuf()->in_avail(), EOF);//flush input buffer } int main() { while(true) { cout << "Exception in this cout on second while" << endl; pause(); } return 0; } 

The essence of the pause () function is simply to delay the execution of the program until the Enter key is entered, or to end the program by pressing the Ctrl + C key (standard combination of closing the program from the console). It seems that the code is ok, I press Enter, it scrolls through the cycle, it releases the buffer, etc. but as soon as I press ctrl + c it doesn’t finish anything, the code comes to the next. cout (in the while loop) and me Visual Studio throws an exception:

First-chance exception at 0x768f6d67 in Lab2.exe: 0x40010005: Control-C.

and the debugger is hanging on Kernel32.dll. Ply with a fright so crap climbs? I tried to remove and rewrite lines in different ways:

 cin.get(); cin.ignore(cin.rdbuf()->in_avail(), EOF); 

vseravno same story, I do not understand these Std. When you press Ctrl + Z then Enter, the program generally enters an infinite loop without stopping at cin.get ().

MB who can help figure out why such miracles occur, it became just interesting to make out the nature of such a reaction of the console and iostream to Ctrl + C and Ctrl + Z

Oh, and Visual Studio 2010 Wednesday

  • In cin.ignore(cin.rdbuf()->in_avail(), EOF); writing EOF redundant. You can omit this argument. It works perfectly under Linux. Ctrl + Z sends the process to sleep, Ctrl + C cuts it. - gecube pm

1 answer 1

Everything looks as if the Visual Studio debugger itself intercepts Ctrl + C and interrupts the execution. There he has a checkmark somewhere in the settings that disables the interception of the Ctrl + C debugger.

  • Hmm, really, the problem with CTRL + C compiled without debugger thanks has disappeared, there is clearly a debugger case, but it is still not clear why CTRL + Z drives the code into an infinite loop - Nikita Sistemny
  • 2
    CTRL + Z. Command undo or undo the last action. You have an eternal cycle, which is limited only to cin, which is canceled. - manking