943,655 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 22726
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 13th, 2007
0

"Press any key to continue" type function?

Expand Post »
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!
Last edited by Ratte; Nov 13th, 2007 at 2:23 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

Can't be done. Change your message to Press ENTER to continue and just use the standard I/O functions.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

how bout using getch() or getche() ?
note. if u're using vc++, both are using leading underscore such as _getch() and _getche()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Garock is offline Offline
5 posts
since Nov 2007
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

I find it interesting that, given the following requirement:
Click to Expand / Collapse  Quote originally posted by Ratte ...
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:

Click to Expand / Collapse  Quote originally posted by Garock ...
how bout using getch() or getche() ?
note. if u're using vc++, both are using leading underscore such as _getch() and _getche()
Click to Expand / Collapse  Quote originally posted by Duoas ...
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...
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

>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.
Last edited by Narue; Nov 13th, 2007 at 12:38 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 13th, 2007
0

Re: "Press any key to continue" type function?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 31st, 2009
0

Re: "Press any key to continue" type function?

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

system("PAUSE");

does it all.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krigath is offline Offline
1 posts
since Jan 2009
Jan 31st, 2009
0

Re: "Press any key to continue" type function?

Click to Expand / Collapse  Quote originally posted by krigath ...
there is a really easy way to do this....

system("PAUSE");

does it all.
he wants something platform independant. That only works on windows.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,485 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Connect to Network?
Next Thread in C++ Forum Timeline: Logical error in finding Palindromes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC