Cabezota
Cabezota

Reputation: 25

How do I keep open the console?

I tried this:

main() {
    int a;
    cout << "Enter a number: ";
    cin >> a;
    cout << a;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

But it didn't work.

Upvotes: 0

Views: 196

Answers (3)

xcdemon05
xcdemon05

Reputation: 1432

The easiest way is to simply place:

system("PAUSE");

wherever you want the pause to be (in your case, in the line above return 0;)

However due to lots of security issues, most would consider the use of system to be bad practice. Instead, try using:

cin.get();

Upvotes: 1

Nikos C.
Nikos C.

Reputation: 51920

You don't need to modify your source in order to do this. This tends to be annoying when you exit the program from other places with exit() or abort(). Most IDEs have an option to keep the console open. Are you using Dev-C++ by any chance? It has an option to pause the console. You can find that option in the environment settings. Unless you're using the outdated version of Dev-C++ from Bloodshed. If so, you should update to the Orwell version: http://orwelldevcpp.blogspot.com

Upvotes: 1

Thomas Matthews
Thomas Matthews

Reputation: 57749

I've always been a fan of using:

std::cout << "Paused. Press Enter to continue.";
std::cout.flush();
std::cin.ignore(100000, '\n');

Upvotes: 0

Related Questions