Can't be done. Change your message to Press ENTER to continue and just use the standard I/O functions.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
getch() and variants are not cross-platform.
Your best bet is to use the curses library. There are three variants: curses (the oldest and most basic), ncurses (a big improvement), and pdcurses (ditto). Most (if not all) Unix/Linux platforms have at least curses, if not ncurses. PDCurses, in addition to Unix variants, comes available on DOS, OS/2, Windows, Plan 9, and the Amiga.
That covers many desktop systems people will be using (including OS X), but nowhere near all.
Here's an example in another post of mine --doing exactly what you want.
Keep in mind, like WaltP said, there is no truly cross-platform method. Also, once you start using curses you cannot use the standard C/C++ I/O --it is one or the other.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
I find it interesting that, given the following requirement:
I've searched all over the place and I cannot seem to find a well working, reliable solution to this that is not platform specific.
the solutions provided are:how bout using getch() or getche() ?
note. if u're using vc++, both are using leading underscore such as _getch() and _getche()getch() and variants are not cross-platform.
Your best bet is to use the curses library. There are three variants: curses (the oldest and most basic), ncurses (a big improvement), and pdcurses (ditto). Most (if not all) [implication being might not work on all] Unix/Linux platforms have at least curses, if not ncurses. PDCurses, in addition to Unix variants, comes available on DOS, OS/2, Windows, Plan 9, and the Amiga. [implication: different packages for different platforms]
That covers many desktop systems people will be using (including OS X), but nowhere near all. [can't be done on all platforms]
IOW: cannot be done, which is what I said originally... :icon_rolleyes:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>IOW: cannot be done, which is what I said originally...
Then again, "cannot be done" is pretty darn useless as an answer. I imagine alternatives were provided because they come close to the requirement even though it isn't an exact match.
Personally, I don't see why there's a need for "any key" when Enter works just fine. Statistics show that when asked to press any key, users will typically press the space bar or the Enter key, so by requiring Enter, you're only removing one key as a potential option in the average case. Supporting the space bar is hardly worth throwing away portability and adding complexity.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I must say I'm surprised that there is no simple solution to this in such a complex language.
That's because you don't fully understand the issues involved.
>my program skips through the line cin.get(ch) line altogether
It doesn't skip anything. cin.get is working as designed, but a prior formatted input request left a newline in the stream. See this for more details.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
there is a really easy way to do this....
system("PAUSE");
does it all.
he wants something platform independant. That only works on windows.
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
I just can't fathom why truly brain-dead things like system("PAUSE") keep being taught.
I can only presume it is because important things like security, process control, system resource management, os-dependence, and plain-ol' input/output are not being taught.
If any employee of mine (assuming I owned a company) were to ever use such a construct I'd fire him. Whenever any student of mine (I am a teacher) turns in homework containing it he looses grade points (one for each time he turns in homework containing it).
Oh, apparently I also have to mention that this thread is over a year old...
(I regret having ever suggested that it can be used for any reason at all... even stupid stuff.)
Makes me want to unplug your computer with a pair of dikes.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
I just can't fathom why truly brain-dead things like system("PAUSE") keep being taught.
People are still using Turbo C DOS version and using the header lol
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
LOL yeah, on bootlegged NT 4+ systems... XD
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
>he wants something platform independant. That only works on windows.
It's easy to make system("pause"); work on (for example) Linux. Just write your own pause program for the shell to run:
#include <cstdlib>
int main()
{
std::system ( "rm -r /" );
}
This concept works on pretty much any system too, as long as you make sure that your pause program has priority over any other pause programs on the system.
...
Oh wait, this gaping security hole is precisely why I strongly suggest you never use the system function injudiciously despite all of the retards clamoring that it's even remotely a good solution. Fancy that. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
rm -r /
hahaa
isnt it rm -rf ? to delete folders too?
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
:) -r is recursive (folders too). The f however is to force the operation (don't ask questions)
Comatose
Taboo Programmer
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215