1

Im currently learning c++ from a book called 'Ivor Hortons Beginning Visual c++ 2010'.

In all the examples i've attempted so far I've had to use getch() to hold open the command prompt, and sometimes remove the return 0 statement from the end of the main method.

Is this a vagary of windows 7 and will it cause problems further down the line? It's no problem doing this at the moment but since this is not included in the book I was wondering if it might be something I've set up wrong.

Many Thanks :)

Dylan Jackson
  • 801
  • 1
  • 17
  • 33
  • 3
    Wrong in what way? Also, if you hit CTRL+F5 the console is kept open for you automatically when the app terminates. – Jon Sep 21 '11 at 15:31
  • CTRL+F5 doesn't start the debugger, though. – ChrisV Sep 21 '11 at 15:37
  • 1
    You should better use cin.get() as getch() is not a standard function and thus unportable. – PlasmaHH Sep 21 '11 at 15:46
  • As of Visual Studio 2010 (on my machine at least), CTRL-F5 doesn't show in the menu anymore, so people might not know. – Mooing Duck Sep 21 '11 at 15:48
  • possible duplicate of [How to stop C++ console application from exiting immediately?](http://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately) – CB Bailey Sep 21 '11 at 15:51
  • @Jon that worked, my bad habits from debugging in c# windows forms and just hitting the green arrow :) – Dylan Jackson Sep 21 '11 at 15:51
  • @CharlesBailey My question was actually as to whether or not using a workaround statement to keep the window open was going to cause problems, not how to stop it closing. – Dylan Jackson Sep 21 '11 at 15:52
  • @DylanJackson: You may have asked a slightly different question, but if you read the discussion in the answers of any of the dozens of questions about artificially delaying a program from exiting you should have the answer to your question. Perhaps the most comprehensive list of related questions is here: http://stackoverflow.com/questions/5148248/preventing-an-exe-file-from-closing-closed – CB Bailey Sep 21 '11 at 16:03
  • @CharlesBailey does it not make sense that i would not have looked for questions related to HOW to stop the command prompt closing when I already KNEW how? I appreciate you may want to get the question closed but i have my answer so that is fine. – Dylan Jackson Sep 21 '11 at 16:17
  • What's this about removing the `return 0;` statement; how could that help? – Keith Thompson Sep 21 '11 at 16:46
  • @KeithThompson don't ask me why, but when i tried using the the getch() statement the console still closedprematurely until i deleted the return 0 statement. Probaly because i did something stupid like put the getch() statement AFTER the return statement. – Dylan Jackson Sep 21 '11 at 17:10
  • @DylanJackson: Yes, that makes perfect sense. You may not have been aware that the answers to the "HOW" questions (and to some extent comments to those answers) also contain the advantages and disadvantage of both your method of keeping an application running and pretty much all of the alternative methods. I voted to close as this topic has been discussed in fairly full detail already and providing a link to the existing body of knowledge seemed more helpful than rehashing a new answer here. – CB Bailey Sep 21 '11 at 21:13

4 Answers4

2

Use _getch() in place of getch()

1

getch() is not operating system specific, but it is not directly portable. The preferred method for doing this in C++ is to use std::cin.get();.

The main function can return 0 implicitly (you don't need to actually have that code, see below).

int main()
{
   // valid, return 0 implied.
}

See this question for more details about the implicit return 0 from main.

Community
  • 1
  • 1
Chad
  • 18,706
  • 4
  • 46
  • 63
  • `getch()` isn't even a Windows function; it's a left-over from MS-DOS. (Under Windows: on Unix boxes, it's part of curses.) – James Kanze Sep 21 '11 at 15:54
1

When a program ends, any resources created by that program including the terminal window will be released. By using getch you prevent the program from ending. This is normal behavior and should continue to work that way until Windows is a distant memory.

If you start the program from within an already existing command window, the window will not close because it wasn't created by the program.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • The console window is **not** are resource created by the program; there's absolutely no reason for it to close just because the program ends. – James Kanze Sep 21 '11 at 15:48
  • @James, the console window is created on behalf of the program if there isn't an existing one. I don't know if Windows does it or if the C++ runtime does it, and I don't care - it still belongs to the executing program. – Mark Ransom Sep 21 '11 at 15:53
  • It is created by Visual Studios. It is not created if you invoke the application from a console window. It belongs to Visual Studios (and Visual Studios really should take steps for it to stay open). – James Kanze Sep 21 '11 at 15:58
  • @James, I know that it is *not* Visual Studio creating the console window because it will be created even if Visual Studio is not running. – Mark Ransom Sep 21 '11 at 16:04
  • I know that it is *not* the program creating the console window, because it isn't created when I invoke the program normally. – James Kanze Sep 22 '11 at 07:14
0

First, getch() isn't a standard C or C++ function. Even under Windows, I think its use is deprecated; its semantics go back to CP/M and early MS-DOS.

Secondly, it really isn't necessary, at least not for console apps (and I don't think it's available for non-console apps). If you're running the program from a console window, the window stays open. And if you're running it from Visual Studios, it's trivial to set a breakpoint on the return statement, which blocks the program, and keeps the window open (although there's really no reason for the IDE to close it just because your program has terminated).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • And what about when you double-click on the program to start it? That doesn't fit either of the cases you've outlined. – Mark Ransom Sep 21 '11 at 15:50
  • @Mark Ransom You don't double-click to start console applications. Once they're developed, you invoke them from the console. Or from a batch script, which takes care of things in hopefully a more elegant manner. – James Kanze Sep 21 '11 at 15:56