C++ Console Help: Show Output and Close on Enter Key

  • Context: C/C++ 
  • Thread starter Thread starter chrisalviola
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around how to prevent a C++ console application from closing immediately after displaying output, specifically focusing on methods to pause the program until the user presses a key. Participants explore various approaches, including using specific functions and commands, and discuss the implications of different methods based on the IDE being used.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant suggests using system("PAUSE"); to keep the console open, while another strongly criticizes this method as a poor practice.
  • Some participants propose using getch() from conio.h or std::cin.get(); as alternatives to pause the program.
  • There is a discussion about the use of header files, with some participants noting that .h may be omitted for standard libraries in certain compilers.
  • One participant emphasizes that the choice of method may depend on the IDE being used, highlighting that different environments handle program termination differently.
  • Concerns are raised about teaching poor programming practices, particularly regarding the use of system() and the potential risks associated with it.

Areas of Agreement / Disagreement

Participants express differing opinions on the appropriateness of using system("PAUSE");, with some advocating for its use while others vehemently oppose it. There is no consensus on the best approach, as various methods are suggested and debated.

Contextual Notes

Some participants mention that the use of conio.h is not standard and may not be available in all compilers. The discussion also touches on the implications of using certain functions and the importance of adhering to good programming practices.

chrisalviola
Messages
80
Reaction score
0
#include <iostream>
using namespace std;

int main ()
{
count << "Hello World!";
return 0;
}

this would produce an output in the console and then closes it automatically its so fast I can't even see it, I know you can let the console display the output show and close only when you press enter key, how do you do it? using this example code. tnks
 
Technology news on Phys.org
What IDE are you using?

I think adding a line like system("PAUSE"); should help you out.
 
Almost all IDEs have some way of displaying the output screen.
Or you could simply get a character from the keyboard as input before terminating the program. I like using the getch() function (conio.h). Time delay functions are also useful.
 
I searched these answers on the net hope these are correct. basically you just add these to stop the output from exiting automatically, hope its correct

#include <conio.h>

int main()
{
// Your program here

getch();
}

#include <iostream>

int main()
{
// Your program here

std::cin.get();
}

#include <iostream.h>
void main()
{ ... /* your program here */
cin.get();
}
 
There are other ways of doing it, but these should do quite well. Still, you could try to find out how to display the output screen in your IDE.
 
Well, since you're not asking for any input the program is over as soon as it prints hello world. So if you want it to wait for you to press a key you need to tell it to get input from the user.
 
ranger said:
What IDE are you using?

I think adding a line like system("PAUSE"); should help you out.

Dear god no! Please do not do this. This is a terrible, awful hack. This has windows execute the pause program after your program is completed. Don't do this. Here's what I recommend, Start->Run "cmd"; Now change directories to where the compiled .exe is located. Type the name of the program, and watch the magic happen.

Code:
Microsoft Windows [Version 6.0.6000]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\Sam>cd Documents

C:\Users\Sam\Documents>cd "Visual Studio 2005"

C:\Users\Sam\Documents\Visual Studio 2005>cd projects

C:\Users\Sam\Documents\Visual Studio 2005\Projects>cd Test

C:\Users\Sam\Documents\Visual Studio 2005\Projects\Test>Test.exe
Pass!

C:\Users\Sam\Documents\Visual Studio 2005\Projects\Test>



Code:
#include <conio.h>

int main()
{
  // Your program here

[B]  getch();[/B]
}

Only if you're using Borland's compiler. Conio.h isn't a standard library.

Code:
#include <iostream>

int main()
{
  // Your program here

[B]  std::cin.get();[/B]
}

This is the most standard way to accomplish what you want.

Code:
#include <iostream.h>
void main()
{ ... /* your program here */
[B]cin.get();[/B]
}

If you're going to go this route, the include should be iostream (Without the .h). Furthermore, you'll need to add std:: before cin, or using std::cin at the beginning of the program. Look up the scope operator for more information.
 
I observed that some codes have the .h on the #include, the .h is suppose to be the library or header file or something, but why is that it works without the .h
 
samgreen said:
Dear god no! Please do not do this. This is a terrible, awful hack. This has windows execute the pause program after your program is completed. Don't do this. Here's what I recommend, Start->Run "cmd"; Now change directories to where the compiled .exe is located. Type the name of the program, and watch the magic happen.

There is nothing wrong with using the pause "program", as you described. I used exactly this when I was using Dev C++. In fact, its recommended by the Dev C++ FAQ. The problem lies not with running pause, but in using system() in the first place. This is why I asked what IDE he's using. As different IDEs have different ways of handling program termination. But obviously something like getch() is a better option.
 
  • #10
chrisalviola said:
I observed that some codes have the .h on the #include, the .h is suppose to be the library or header file or something, but why is that it works without the .h

The files that you may omit the .h from, are the standard files included in your implementation of c++. It is pretty compiler specific, but you can count on using all the STL headers without the .h.

ranger said:
There is nothing wrong with using the pause "program", as you described. I used exactly this when I was using Dev C++. In fact, its recommended by the Dev C++ FAQ. The problem lies not with running pause, but in using system() in the first place. This is why I asked what IDE he's using. As different IDEs have different ways of handling program termination. But obviously something like getch() is a better option.

There is nothing wrong with it in this instance per se, but why teach poor programming practices? System is a very poor implementation. Let us take a hypothetical (albeit far fetched) situation where the program "pause" has been replaced by a virus of the same name. Instead of your program pausing so you can see the results as you expected, you have no executed unintended, malicious code.

http://cpp.codenewbie.com/articles/cpp/1437/systemPAUSE-Page_1.html
 
Last edited by a moderator:

Similar threads

  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
4K