Posted By: Anonymous
Lately, I’ve been trying to learn C++ from this website. Unfortunately whenever I try to run one of the code samples, I see that program open for about a half second and then immediately close. Is there a way to stop the program from closing immediately so that I can see the fruits of my effort?
Edit: As Charles Bailey rightly points out in a comment below, this won’t work if there are characters buffered in
stdin, and there’s really no good way to work around that. If you’re running with a debugger attached, John Dibling’s suggested solution is probably the cleanest solution to your problem.
That said, I’ll leave this here and maybe someone else will find it useful. I’ve used it a lot as a quick hack of sorts when writing tests during development.
At the end of your
main function, you can call
This will get a single character from
stdin, thus giving you the “press any key to continue” sort of behavior (if you actually want a “press any key” message, you’ll have to print one yourself).
You need to
#include <cstdio> for