954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

C++ Console Pause Function

By packetpirate on Sep 8th, 2010 3:32 am

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

// 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.
    */
}

This Not working for me Iam working on visual stdio 2005

prvnkmr449
Junior Poster
106 posts since Sep 2010
Reputation Points: 3
Solved Threads: 16
 

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

chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 

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.

packetpirate
Junior Poster in Training
60 posts since Jun 2010
Reputation Points: 10
Solved Threads: 3
 

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.

chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 

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

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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();
chococrack
Junior Poster
149 posts since Oct 2008
Reputation Points: 92
Solved Threads: 16
 
#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

seanbp
Junior Poster
129 posts since Jul 2010
Reputation Points: 20
Solved Threads: 15
 

n00bs all up in this thread.

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

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 
#include #include #include #include


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


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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: