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.

Here is what I have:

Main menu of a class (while loop with 0 as exit).
User selects choices.
Choice calls class function to carry out the specific request.

I want the result of each of the functions to stay on screen until the user presses any key (and doesn't have to press enter to confirm it) and then the menu is called again.

Is there an easy way to do this?

PS. I've seen an implementations with cin.get(ch) or cin.ignore(1,0), but neither work in my situation. After a certain function is done, the menu is called as if bypassing the get character line completely.

Thanks!

Recommended Answers

All 15 Replies

Can't be done. Change your message to Press ENTER to continue and just use the standard I/O functions.

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) 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.

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:

>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.

I must say I'm surprised that there is no simple solution to this in such a complex language.

Actually, ENTER would work just as well for me, however, I do not understand why, but my program skips through the line cin.get(ch) line altogether, so it doesn't ask for anything when it should.

>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.

there is a really easy way to do this....

system("PAUSE");

does it all.

there is a really easy way to do this....

system("PAUSE");

does it all.

he wants something platform independant. That only works on windows.

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.

commented: Sanity when teaching ! Thx! +4

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 <iostream.h> header lol

LOL yeah, on bootlegged NT 4+ systems... XD

>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:

rm -r /

hahaa

isnt it rm -rf ? to delete folders too?

:) -r is recursive (folders too). The f however is to force the operation (don't ask questions)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.