• Advertisement
Sign in to follow this  

"Pausing"

This topic is 4402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a better method than system("PAUSE") to make a program pause for a key. Someone told me that system("Pause") isn't good for windows programs. I am just programming the consol now, but I think it would be best to kick the habit now.

Share this post


Link to post
Share on other sites
Advertisement
Well, it depends on where and why you're pausing. If you're pausing at the end of the program to see the output, then it is kind of bad.

The reason is because console programs are supposed to be run through the console. When a program finishes, the console is still there, so the "pause" is not really necessary to keep the console window there. None of the debuggers I use run the program "properly" (ie, don't terminate until the console window is closed), so I end up doing system("pause") or cin.ignore() I think*.

Personally, I think the best solution is to have them in there in the debug binary, but not in the release version. The easiest way to do this is to use a preprocessor condition -
#ifdef _DEBUG
system( "pause" );
#endif

_DEBUG should be defined by your compiler when you build in debug mode. When you switch over to release mode, that line will be removed by the preprocessor for you, and no one will know that its there.

Everyone has wildly different opinions on this, so expect someone to say differently after me. In the end, it all comes down to programmer preference, but if you plan for your application to be run within a "real" console window, its a lot better to leave the pause out (for aesthetic purposes).

* been looking through STL docs and haven't found it. Another way to do it using cin and not system is to write cin >> foo; which will block until something it typed.

Share this post


Link to post
Share on other sites
cin.ignore() works too.

The "problem" with system("pause") is that it's not C++, it calls an external program (pause is just a program like, say, dir or copy. Try typing it in at the command line)
So it's hardly the most elegant solution to anything. But hey, it works. On Windows, at least. But because it just calls an external program, it only works on platforms where pause actually exists.

Share this post


Link to post
Share on other sites
In Visual Studio, running the program by pressing Ctrl+F5 keeps the window open when the program ends. However, it doesn't run the debugger if you do it that way (IIRC).

Share this post


Link to post
Share on other sites
There are a lot of ways:

The best way (not really best just more...correct way) is to run the program from the command line (cmd.exe).

Then there are the things like:
getch()
getchar()
other stuff that I can't remember...

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmarobo
Ah, thanks I'll try cin.ignore. It's just a performance issue right?

Not even - its an aesthetic issue for the most case.

Share this post


Link to post
Share on other sites
It is indeed about aesthetics. Who's really going to care about performance if you intend to wait for someone to hit a key, anyway? :) It really is ugly, though. It also interferes with attempts to use *your* program in a batch script.

If you make a batch script that runs your program and then runs 'pause', for example, then users are free to also *just* run your program (which they might want to do if they already have a console window open). Whereas if your program hard-codes a "pause" invocation, then suppose someone wants to run your program with input redirected from a file, then run another program to process the file. Now it's impossible to have a fully automated run - someone has to press enter at the appropriate time, because the file-redirected input is fed to *your* program, not to 'pause'. Maybe that's not so important for "games", but for "tools" it's critical, and it's just plain good programming practice in general.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement