'Program runs infinitely on typing ctrl C instead of stopping in c++

I have the following code. It runs infinitely after typing ctrl + c and executes the default section of code.

// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;

int main() {
    char oper;
    float num1, num2;
    while(true){
        cout << "Enter an operator (+, -, *, /): ";
        cin >> oper;
        cout << "Enter two numbers: " << endl;
        cin >> num1 >> num2;

        switch (oper) {
            case '+':
                cout << num1 << " + " << num2 << " = " << num1 + num2;
                break;
            case '-':
                cout << num1 << " - " << num2 << " = " << num1 - num2;
                break;
            case '*':
                cout << num1 << " * " << num2 << " = " << num1 * num2;
                break;
            case '/':
                cout << num1 << " / " << num2 << " = " << num1 / num2;
                break;
            default:
                // operator is doesn't match any case constant (+, -, *, /)
                cout << "Error! The operator is not correct";
                break;
        }
        cout<<endl;
    }
    return 0;
}

The result after typing ctrl + c is as shown in the image. enter image description here

So my question is: is it normal behavior in c++? If it is, then, how may we interpret it?



Solution 1:[1]

If you want to capture CTRL-C in windows, you cannot capture it like linux by signal(). signal() does not handle SIGINT correctly in windows. You need to use SetConsoleCtrlHandler for this purpose.

If you don't set handler, application may decide to ignore CTRL-C. CTRL-BREAK is always a signal, but CTRL-C maybe be not:

CTRL+BREAK is always treated as a signal, but typical CTRL+C behavior can be changed in three ways that prevent the handler functions from being called:

1- The SetConsoleMode function can disable the ENABLE_PROCESSED_INPUT mode for a console's input buffer, so CTRL+C is reported as keyboard input rather than as a signal.

2- Calling SetConsoleCtrlHandler with the NULL and TRUE arguments causes the calling process to ignore CTRL+C signals. This attribute is inherited by child processes, but it can be enabled or disabled by any process without affecting existing processes.

3- If a console process is being debugged and CTRL+C signals have not been disabled, the system generates a DBG_CONTROL_C exception. This exception is raised only for the benefit of the debugger, and an application should never use an exception handler to deal with it. If the debugger handles the exception, an application will not notice the CTRL+C, with one exception: alertable waits will terminate. If the debugger passes the exception on unhandled, CTRL+C is passed to the console process and treated as a signal, as previously discussed.

Here is the msdn document.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1