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
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:
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( constchar* 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
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();
}
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.
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).
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.