Looking for methods to a exit a program

Hiya, folks.

My quandary is fairly simple. I'm looking for ways to set the program so that it may not exit with any button press, and instead exit at will exit with the correct string input.

I'm unsure where to begin with programming effects that affect the application itself, and not just what's in the window. Any help would be appreciated!
We need more context in order to understand the question.

In general, you'll want to return from main to cleanly exit the application.
Last edited on
I believe the OS matters here. For example in windows you will need to trap ctrl-c AND alt-f4 and possibly other things and reroute them to do nothing, and it depends also if its a console or gui program, and preventing closing the console itself via the x button, etc. Depending on how stubborn you want your program to be about it, this can get involved.

Its not hard to make a program resist being closed by accident, but to prevent all normal methods (not counting kill command / taskman) is another level.
Last edited on
I think we're overthinking it here.

How about something like,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Example program
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    while (true)
    {
        cout << "Enter 'apple' to terminate program: ";
        string str;
        cin >> str;
        
        if (str == "apple")
        {
            break;   
        }
        else
        {
            cout << "Incorrect.\n";   
        }
    }
}


Yes, if you actually really have a good reason to prevent signals like SIGKILL or whatever from interacting with the program, then that requires some OS-specific hackery.
Topic archived. No new replies allowed.