| Duoas (6752) | ||||||||||||||||||||
This is typically a problem on Windows, caused by really dumb IDEs that don't know enough to keep the console open after the program finishes. However, it does strike right at one of the main philosophical problems with the way you are thinking about programming. After all, a console program should be run from the console --else once it terminates, the console should disappear. We'll get to fixing that later. For now, we'll stick with the immediate problem.
While simple, it really is a Bad Thing. See Why system() is evil for more.
It is also Windows-only. To do the same thing in Linux, you'd need to use the read shell command
The most correct way to do it is something like:
Thereafter, simply place a call to it at the end of your program:
The main problem with this approach is that it only works when input is properly synchronized. If it isn't, you will need to Flush Stdin. Learn more about proper synchronization with Issues When Reading User Input. Also, it requires the user to press the Enter key to continue. This isn't such a bad problem as you might think. The mythical Any key has befuddled a lot of people. (A better phrase would have been "Press a key to continue..." --at worst people will press the A key.) Remember, non-programmers are almost always your target audience. And even if they aren't, what you are thinking isn't often as obvious as you think. Make sure to give clear instructions to the user about what kind of input your program wants next.
The Curses library is designed for working with the console. Advantages: it is cross-platform. Disadvantages: it doesn't interact well with the standard streams. In other words, you shouldn't mix printf(), etc or cout, etc with curses. Use one or the other, not both.
So if all you want to do is pause your program once or twice then using Curses is overkill.
Well, first some code.
The caveat is that it is non-standard, meaning that the actual functions it provides vary a lot and they don't always behave just right. (Microsoft invented it years ago. That's right, not Borland --though Borland is [in]famous for it). Hence, for anything other than Windows programs it is also a sub-optimal solution. See Using <conio.h> for more. | ||||||||||||||||||||
|
Last edited on
|
||||||||||||||||||||
| Duoas (6752) | ||||||||
To do it any better, you need to do something OS-specific. Part of the design philosophy behind C and particularly C++ is something to the effect that the OS doesn't exist (or if it does, it is a magical black box). So to do something special with the console (C and C++ don't know what a console is, remember) means you will have to write code that is special to your operating system. Here is a function that really lets you press any key, and returns the key that was pressed. Windows provides some nice, simple functions for playing with the console. It takes a little more magic on Linux, but it is just about as easy. Windows
POSIX (Unix, Linux, Mac OSX, etc)
If you are using C++, you can add some convenience this way:
Well, that's enough of that. To learn more about unbuffered (AKA "raw") input, take a gander at Read a single key for input. Well, that's enough for now. | ||||||||
|
Last edited on
|
||||||||