views:

754

answers:

7

Hi, my question is super simple, but I'm transitioning from c# to c++, and I was wondering what command holds the console window open in C++?

I know in C#, the most basic way is:

Console.ReadLine();

Or if you want to let the user press any key, its:

Console.ReadKey(true);

How do you do this in c++? The only reason I ask this simple of a question here, is that I haven't been able to find a good and clear answer out there on the internet.

Thanks!

+4  A: 

How about std::cin.get(); ?

Also, if you're using Visual Studio, you can run without debugging (CTRL-F5 by default) and it won't close the console at the end. If you run it with debugging, you could always put a breakpoint at the closing brace of main().

Cogwheel - Matthew Orlando
is std::cin.get() only a member of namespace std? If i include <iostream>, could I just go: cin.get()?
Alex
If you have `using namespace std;` in your source file, then yes.
Cogwheel - Matthew Orlando
Or `using std::cin;` for that matter
Cogwheel - Matthew Orlando
So, I don't even need a command like this, because the console automatically stays open if not debugging? That's nice.
Alex
Yep. Works for .Net apps too.
Cogwheel - Matthew Orlando
A: 

if you create a console application, console will stay opened until you close the application.

if you already creat an application and you dont know how to open a console, you can change the subsystem as Console(/Subsystem:Console) in project configurations -> linker -> system.

ufukgun
A: 

Roughly the same kinds of things you've done in C#. Calling getch() is probably the simplest.

Jerry Coffin
A: 

In windows, you can use _getch() in the

<conio.h>

header.

DanDan
This will work on several windows compilers, but be warned: it isn't part of the standard, and isn't portable.
luke
A: 

You can also lean on the IDE a little. If you run the program using the "Start without debugging" command (Ctrl+F5 for me), the console window will stay open even after the program ends with a "Press any key to continue . . ." message.

Of course, if want to use the "Hit any key" to keep your program running (i.e. keep a thread alive), this won't work. And it does not work when you run "with debugging". But then you can use break points to hold the window open.

Daver
A: 

A more appropriate method is to use std::cin.ignore:

#include <iostream>

void Pause()
{
   std::cout << "Press Enter to continue...";
   std::cout.flush();
   std::cin.ignore(10000, '\n');
   return;
}
Thomas Matthews
Appropriate in what way?
Scottie T
+1  A: 

The right way

cin.get();

cin.get() is C++ compliant, and portable. It will retrieve the next character from the standard input (stdin). The user can press enter and your program will then continue to execute, or terminate in our case.

Microsoft's take

Microsoft has a Knowledge Base Article titled Preventing the Console Window from Disappearing. It describes how to pause execution only when necessary, i.e. only when the user has spawned a new console window by executing the program from explorer. The code is in C which I've reproduced here:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hStdOutput;
BOOL bUsePause;

void main(void)
{
        hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        if (!GetConsoleScreenBufferInfo(hStdOutput, &csbi))
        {
                printf("GetConsoleScreenBufferInfo failed: %d\n", GetLastError());
                return;
        }

        // if cursor position is (0,0) then use pause
        bUsePause = ((!csbi.dwCursorPosition.X) &&
                     (!csbi.dwCursorPosition.Y));

        printf("Interesting information to read.\n");
        printf("More interesting information to read.\n");

        // only pause if running in separate console window.
        if (bUsePause)
        {
                int ch;
                printf("\n\tPress any key to exit...\n");
                ch = getch();
        }
}

I've used this myself and it's a nice way to do it, under windows only of course. Note also you can achieve this non-programatically under windows by launching your program with this command:

cmd /K consoleapp.exe

The wrong way

Do not use any of the following to achieve this:

system("PAUSE");

This will execute the windows command 'pause' by spawning a new cmd.exe/command.com process within your program. This is both completely unnecessary and also non-portable since the pause command is windows-specific. Unfortunately I've seen this a lot.

getch();

This is not a part of the C/C++ standard library. It is just a compiler extension and some compilers won't support it.

Mike Weller