3

I'm sort of learning/teaching myself C++ using the internet and Bjarne Stroustrup's "Programming: Principles and Practice Using C++", and I know that you can keep the console window open by using either system(PAUSE), cin.get(), or this keep_window_open() that Stroustrup mentions, but I was wondering which is the best way, or at least the most accepted by programmers.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
nworb99
  • 33
  • 4
  • Could you expand on why you want to keep the window open? – ctrlc-root Nov 21 '15 at 02:01
  • It's mostly for debugging. I suppose for simple beginning programs I'm doing like tic tac toe and things, I like to make sure everything is working correctly (i.e. no typos, arrays working correctly) before it exits. Is it better practice to just let it close when it finishes? – nworb99 Nov 21 '15 at 02:10

1 Answers1

2

Don't use any of these tricks.

Instead of cluttering your program with constructs that make it not exit when it actually is done, learn how to use your terminal emulator in a way that is useful for your task. I think on most emulators, if you open the terminal window up-front, then navigate to the directory where the executable is (or will be) and then launch it by its name, the window will stay open after the program finished. It is only when you launch the executable by double-clicking on it that the window will close immediately.

If you're launching the program from within your IDE, check if it has some option to keep the window open. How this is done will of course depend on the IDE. (See the comments for some pointers.)

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
  • Does this work for debugging in an IDE like Visual Studio Community, or just when actually running the program? – nworb99 Nov 21 '15 at 02:11
  • In visual studio you could simply put a breakpoint at the last line in the main() function, – Minor Threat Nov 21 '15 at 02:12
  • @nworb99 alternatively, you could use Ctrl+F5 and it will keep the window open http://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio – ctrlc-root Nov 21 '15 at 02:15
  • @root.ctrlc would I press Ctrl + F5 when I start debugging or as it's debugging? – nworb99 Nov 21 '15 at 02:21
  • @nworb99 I think you do it before you start running the program in Visual Studio. I don't have a copy handy to verify this but that's what it seems like from the answer I linked. – ctrlc-root Nov 21 '15 at 02:23
  • @root.ctrlc I just tried pressing Ctrl + F5 (which I didn't know was a shortcut for debugging, thanks) but it didn't stay open. Also, thanks for linking the other answer, I guess my question was pretty trivial and a duplicate whoops. I'll read that question carefully. Thanks for all the help, guys, hopefully my next question will be more advanced. – nworb99 Nov 21 '15 at 02:30
  • @nworb99 I'm actually glad you've asked this question because *many* new programmers put that `system("pause");` stuff in their code and now we have a place to direct them for superior alternatives. I just cannot give any advice about Visual Studio which I don't use. – 5gon12eder Nov 21 '15 at 02:37
  • Okay wait I read the other question, and Ctrl + F5 works when you change the subsystem to console, but when I try to debug it with a breakpoint it doesn't quite work properly. The console window stays open and the cursor flashes, but pressing keys won't do anything and I have to "x" out of the console window to stop it. Is that supposed to happen? – nworb99 Nov 21 '15 at 02:44
  • @nworb99 `F5` is for running with the Debugger attached. `Ctrl + F5` is for running *without* the Debugger attached. (No breakpoints will work. Although, your code will run faster.) – Casey Nov 21 '15 at 03:28
  • @Casey thanks, and I figured it out. When I debug with a breakpoint, it hides the console behind the actual IDE window, and the reason it would freeze is I wasn't realizing that I would have to continue the debugging from the IDE window for it to close properly. – nworb99 Nov 21 '15 at 18:09