Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ code to automatically close console

Tags:

c++

console

I am currently learning C++ for a programming course. I noticed that my professor's program automatically closes at the end of their program. It usually prompts the user for an input, and then upon entering an input, the program closes. How would I code that? I only know using return 0 gets me "Press Any Key to Continue"

Note: this is a .exe file

like image 581
Andres Lamberto Avatar asked Mar 22 '26 16:03

Andres Lamberto


2 Answers

If your program doesn't wait for any input, it runs and finally exits from the program. On exiting, the console automatically closes. I assume, to run the program you're clicking on the .exe, as opposed to runnng the program from cmd.exe, or you run the program from visual studio itself without debugging.

like image 95
Nawaz Avatar answered Mar 25 '26 06:03

Nawaz


You could just put the following line before return 0;:

std::cin.get();

It will wait for some input and then proceed.

like image 28
Morten Kristensen Avatar answered Mar 25 '26 07:03

Morten Kristensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!