Any alternatives to system("PAUSE")?

I just read an article that clearly explained why system("") commands are bad to use. The command system("PAUSE") is a very useful command in my eyes, so are there any alternatives to this command that perform the same operation?
Thanks
Just use cin to wait for the user to input something.

What is it you're actually trying to achieve?
closed account (zb0S216C)
One of the most common -- and one of the safest -- methods of pausing a console is the following:

 
std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' );

This line simply removes all characters within the "cin" stream until it either encounters a new-line or until all characters are removed from the stream. This can be simplified with a simple function call:

1
2
3
4
5
6
7
8
9
10
11
int PauseConsole( int ReturnCode )
{
  std::cin.ignore( ( std::numeric_limits< std::streamsize >::max )( ), '\n' );
  return( ReturnCode );
}

int main( )
{
  // ...
  return( PauseConsole( 0 ) );
}

Another method of pausing the console is to run the program from command-prompt (Windows) or the terminal (*nix).

Wazzak
Last edited on
the main reason why system() is considered bad, is because this function is platform depending.
the result of this function when ran on windows is way different than when ran on Linux.
you shouldn't say:
i'm gonna stick with windows, and keep using this function.

if you work on a program with a team, your code should be cross platform, because you don't know what platform is the other members are using, and you might eventually compile the code for multiple operating systems.
i was looking for the alternative that some friend developed manually for pausing the console and is cross platform, looks like i missed it.
instead of a link, here's the full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifdef max	//	windows.h library defines a function-like macro with the name of max()
#define _TEMP_MACRO_ max	//	store the predefined macro in a new one
#undef max	//	undefine the problamatic macro.
#endif
void pause( const char* prompt )
{
    std::cin.clear() ; // clear failed/error states of the stream if they are set

    if( std::cin.rdbuf()->in_avail() ) // if there are any characters in the input buffer
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ; // throw them away

    std::cout << prompt ;
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;
}

#ifdef _Temp_MACRO_
#define max _TEMP_MACRO_	// restore the max() macro.
#undef _TEMP_MACRO_	// undefine the temporary macro.
#endif 
Last edited on
I'm trying to use system("PAUSE") to pause the console so the user can see the output before the console shuts down.
Any reasonable IDE will have the option to leave the console open after running. Or you can add some input at the end so the program waits for that. Or just run it from the command line.
Before yet another alternative to what has been mentioned above, let me say that if you are really creating an application that is meant to be run inside a terminal, then most of the time, having the program pause is a hinderance.
Why? Well, these programs are usually run by typing the names into a terminal screen, therefore the screen will still be up after your program has ran. Also, scripts can use your program, but they will freeze and wait for your program to finish if your program pauses.

I'm not saying it isn't useful when you're writing your own program for your personal useā€¦ It's just 99% of the time, it shouldn't be used in programs others will use.

1
2
3
4
5
6
7
void myPause()
{
	std::cout << "Press ENTER to continue..."; 
	std::cin.clear();
	std::cin.sync();
	std::cin.get();
}
Last edited on
Thank you all. I used BlakeK's solution in this program, but the other two solutions will be helpful in other situations. Thanks again guys.
@BlakeK:
can you explain this line:
std::cin.sync();

this reference:
http://www.cplusplus.com/reference/istream/istream/sync/
mentioned:
Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true). Then (if good), it calls pubsync on its associated stream buffer object (if rdbuf is null, the function returns -1 instead). Finally, it destroys the sentry object before returning.


i checked on pubsync(), this is what i found:
http://www.cplusplus.com/reference/streambuf/streambuf/pubsync/
Calls the protected virtual member sync.


i followed that even deeper:
http://www.cplusplus.com/reference/streambuf/streambuf/sync/
Its default behavior in streambuf is to do nothing and return zero (indicating success), but derived classes that use intermediate buffers shall override this behavior to properly synchronize them: filebuf overrides this virtual member function (see filebuf::sync).


am i missing something here ?


EDIT - did you check the above:
http://www.cplusplus.com/forum/beginner/106769/#msg578325
Last edited on
> for pausing the console and is cross platform
> instead of a link, here's the full code: ...

Lest someone be completely misled, this is the link: http://v2.cplusplus.com/forum/beginner/105772/#msg571520

Which clearly states: There is no portable way of emulating the behaviour of system("PAUSE")

The code that Rechard3 posted happens to work on the Microsoft implementation.
I am using windows 7 partitioned with ubuntu, but I have a separate device that runs backtrack 5 r3. Also, yes Blake I looked up the syntax meaning of those lines. Thanks again.
Topic archived. No new replies allowed.