-1

Possible Duplicate:
How to stop C++ console application from exiting immediately?

I'm currently learning C++ and cant figure out how to stop the application from exiting when I want to show the user some data, and have time to read it. How may i do that?

Update: Somehow I pause the application and when the user click a button it exits.

Community
  • 1
  • 1
Stian
  • 649
  • 3
  • 7
  • 15

3 Answers3

2

I assume you're on Windows.

This can turn into a long debate, but generally there are two main ways people do it:

  1. (recommended) Add cin.get(); to the end of your program. This will cause your program to stop until the user hits enter, then it will continue and quit.

    • This will work on Windows and Linux.
  2. Add system("PAUSE"); at the end of your program. This will cause your program to print something like Press any key to continue. . . and when you hit a key, the program will continue and quit.

    • There is a chance that the PAUSE command doesn't do that and can order pizza, launch the missiles, or something else.

    • This only works for Windows but you won't need it when you go to linux anyway.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 4
    3. Run the applicaton from command line – Adam Trhon Nov 13 '11 at 19:44
  • Inside the int main() or after all code? – Stian Nov 13 '11 at 19:45
  • @StianOlsen inside `int main` after all the other code inside it. – Seth Carnegie Nov 13 '11 at 19:46
  • These are really both bad options. The first will make his program useless in pipelines. The second will make his program useless in pipelines and also non-portable. Why not teach the right way? – David Schwartz Nov 13 '11 at 19:50
  • 1
    @DavidSchwartz From the nature of this question, I think it's safe to assume that he won't be using his program in pipelines. – Seth Carnegie Nov 13 '11 at 19:51
  • @SethCarnegie If he becomes another of the 75,000 people who put `system("pause");` at the end of all his programs, he surely won't be using his programs in pipelines. But still -- why not teach the right way? Why teach two wrong ways? I don't get it. – David Schwartz Nov 13 '11 at 19:52
1
std::cout << "Press enter to exit" << std::endl;
cin.get();
exit(1);
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • Yeah, I was trying to remember the behavior - I don't do a lot of input from `stdin` these days. I forget the terminal is going to buffer until `return` is hit. – Brian Roach Nov 13 '11 at 19:48
0

If you want to pause your program until the user presses a key, you can use cin.get(). But really, the program should terminate when it's finished. Otherwise, it's useless in pipelines and the like.

If your program's window goes away when it's finished, then something is wrong with the way you launched it. Whether the window stays when the program is done is up to the thing that launches the program and sets up the window, not the program. This problem is usually caused by launching CLI programs from a GUI. If you want to launch from a GUI, write a GUI program. Otherwise, launch terminal programs from the terminal.

Do not use system("pause");. It makes your program unportable. And if you ever run it on a system whose pause command does something other than what you want, you may have a very angry user on your hands.

Do not use getch. That's a Windows console function and a Curses function. It's not part of C++. (Unless you are specifically developing for a platform that has this function and you are sure it does what you want. But this one function would be a really silly reason to make your program unportable.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • The program is getting data from the user and i calculate it to a result and i want the user to see the result but the application exits immeditaly after the inputs and click enter, its just show the result for 0,3 seconds and exits. How may i prevent that on windows? – Stian Nov 13 '11 at 19:54
  • @StianOlsen Launch the program from a terminal or command line rather than from the GUI. If the program is *necessarily* interactive, you can use `cin.get()`. (For example, if it has menus and no option to disable them or if it mixes prompts with its output.) – David Schwartz Nov 13 '11 at 19:55