This is for the numerous people I see constantly asking how to "pause" the console after their code runs. The explanation for how the code works is within the snippet in comments.

Basically, the "cin.clear();" function will clear the input stream, getting rid of any newline characters that would have made methods like using the "cin.get();" function to pause the console not work. Then, we have the "cin.ignore();" function. This will ignore the maximum amount of characters that can be entered into the stream (numeric_limits<streamsize>::max) be ignored, or wait for a newline character ( \n ). This way, the console will be "paused" until you enter a newline character (or the max. amount of characters for the input stream, but that would take forever and be unnecessary.)

I hope this helps all of you C++ beginners who are either learning for yourselves or just getting started in a college course.

Comments
In fact you can do in a easier way
// C++ Pause Function

#include <iostream>
using std::cout;
using std::cin;
#include <limits>
using std::numeric_limits;
using std::streamsize;

void pause();

int main()
{
    cout << "This is a test of the pause function...\n";

    pause();

    return 0;
}

void pause()
{
    cout << "Press <ENTER> to continue . . .";

    // Clears the input stream.
    cin.clear();

    /* This will tell the console to wait for the maximum
       number of characters that can be entered into the
       input stream or for a newline character, hence, the
       <ENTER> key.
    */
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    /* This has been tested in various programs throughout
       my C++ course in college and has worked every time.
    */
}

If you're in windows (like using visual studio 2005) then just use

system("PAUSE");

If you're on a Unix console, you really don't need a pause after you run your code.

The snippet above may be useful for pausing at different times other than after the code runs, to run the program in chunks for example

Comments
using system("PAUSE") ?? bad !!

First of all, no... never ever use the method Chococrack mentioned. It is for amateur programmers and a shitty habit to learn. Second... how are you using it? Show me your code.

I'll use the one line system("PAUSE") or simple cin.get(); in smaller "amateur" code that I test things on.

For professional projects, I tend to use my IDE of choice's built in debugging tools (like breakpoints in VS).

The function you posted works fine, as does the simple one line I posted.

Flaming another contributors post is definitely not the way to go about gaining a great reputation here.

@OP
The function ios::clear() does not empty a stream. If the stream is in an error state it resets the stream's error flags to return it to a usable state. You still need to empty the stream by another method such as istream::igmore().
This method appears to work. But unless I'm misreading something your understanding appears to be flawed.

@chococrack
The system() function is a gaping security hole that must not be used unless absolutely necessary. Its use is ill-advised under any conditions unless absolutely necessary...

Edited 6 Years Ago by Fbody: n/a

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <iostream>

int getch();

int main(){
	using namespace std;
        char i;
        while (getch() != 10);

}

int getch() {
    int ch;
    struct termios oldt;
    struct termios newt;
    tcgetattr(STDIN_FILENO, &oldt); /*store old settings */
    newt = oldt; /* copy old settings to new settings */
    newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */
    tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */
    ch = getchar(); /* standard getchar call */
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */
    return ch; /*return received char */
}

http://ubuntuforums.org/archive/index.php/t-225713.html

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <iostream>

This won't work at all using most compilers. I dare you to try it on VC++. Using O/S and compiler dependent solutions are problematic. These include
* system("pause"); * Any Linux/Unix-only solutions: unistd.h and termios.h * Non-standard C function getch() , defined only in particular compilers on one O/S


The BEST answer is the original post in this thread. Please learn and understand why. Don't just post your favorite inadequate solution as the best way. In these forums we try to teach the proper way to do things, not the hackneyed way most people are taught by less-than-adequate programmers.

More here

Flaming another contributors post is definitely not the way to go about gaining a great reputation here.

There was no flame. The suggestion of system("pause"); is shitty. Nothing against you, just that suggestion.

cout + cin.get() is all you need in C++
printf() + getchar() is all you need in C

printf() is a huge hit on resources. If all you are doing it printing a character or a string, use putchar() and puts() repectively. :icon_wink: