Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Hold the console window open?

My question is super simple, but I'm transitioning from C# to C++, and I was wondering what command holds the console window open in C++?

I know in C#, the most basic way is:

Console.ReadLine(); 

Or if you want to let the user press any key, its:

Console.ReadKey(true); 

How do you do this in C++? The only reason I ask this simple of a question here, is that I haven't been able to find a good and clear answer out there on the internet.

like image 268
Alex Avatar asked Dec 15 '09 16:12

Alex


People also ask

How do I keep the console open?

Keep Console Open With the Ctrl + F5 Shortcut in C# The best approach for keeping our console window open after the execution of code is to run it with the Ctrl + F5 shortcut of the Microsoft Visual Studio IDE.

How do I keep the console window open in Visual Studio?

Start the project with Ctrl + F5 instead of just F5 . The console window will now stay open with the Press any key to continue . . . message after the program exits.

How do you hold console in C++?

You can also lean on the IDE a little. If you run the program using the "Start without debugging" command (Ctrl+F5 for me), the console window will stay open even after the program ends with a "Press any key to continue . . ." message.

How do I stop my console window from closing?

You can simply press Ctrl + F5 instead of F5 to run the built code. Then it will prompt you to press any key to continue. Or you can use this line -> system("pause"); at the end of the code to make it wait until you press any key.


1 Answers

How about std::cin.get(); ?

Also, if you're using Visual Studio, you can run without debugging (CTRL-F5 by default) and it won't close the console at the end. If you run it with debugging, you could always put a breakpoint at the closing brace of main().

like image 166
Cogwheel Avatar answered Oct 04 '22 14:10

Cogwheel