Small tool to terminate console

Hey guys i was reading the console closing down thing and in another topic(I made) someone recommended me to NOT use system("pause") thingy so i followed that step but i dont want to use cin.ignore() or the other one neither so i wanted to someone give me a small tool to terminate the console like system pause pressing a key but without IT.

NOTE: Im happy =D ahahaha
Why don't you open a console manually and then run the program from there? The console stays alive after it finishes.
Well, that's inconvenient I guess.

Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <limits>

int halt(int, int);

int main() {
    halt('a', 0);
    return halt(0x1B, 0);
}

int halt(int key, int rtnVal) {
    if (key == 0x1B) { // Escape key. If we are sent the escape key, we exit. Otherwise we try to find the enter key.
        exit(rtnVal);
    } else {
        std::cout << "Press enter to continue...\n";
        std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n'); /* Ignore input until maximum amount of
                                                                                  characters read or enter pressed */
        return rtnVal;
    }
}


That would close the console if you wanted it to (by sending it the value of Escape, 0x1B or 1Bh (hex)). I just tested it and it works... so yeah.
Last edited on
Topic archived. No new replies allowed.