C++ Console Pause Function

packetpirate -1 Tallied Votes 2K Views Share

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.

FreeBirdLjj commented: In fact you can do in a easier way +0
// 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.
    */
}
prvnkmr449 -8 Junior Poster

This Not working for me Iam working on visual stdio 2005

chococrack 74 Junior Poster

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

dmt.Arsenal commented: using system("PAUSE") ?? bad !! +0
packetpirate 0 Junior Poster in Training

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.

chococrack 74 Junior Poster

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.

Fbody 682 Posting Maven Featured Poster

@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...

chococrack 74 Junior Poster

Found a beautifully written post regarding how to do this by Narue:

http://www.daniweb.com/forums/post106219.html#post106219

The suggestion is
clear the input stream of junk and use

cin.get();
seanbp 4 Junior Poster
#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

MosaicFuneral 812 Nearly a Posting Virtuoso

n00bs all up in this thread.

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

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

#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:

MosaicFuneral 812 Nearly a Posting Virtuoso

Ah, I've been p0wnd. I submit to your experience.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.