| | |
"Press any key to continue" type function?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!
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.
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.
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:
the solutions provided are:
IOW: cannot be done, which is what I said originally...
•
•
•
•
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.
•
•
•
•
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]
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
>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.
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.
I'm here to prove you wrong.
>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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- Need help with writing function to populate the array (C)
- i++ and ++i (C)
- 100% CPU usage & hardware interrupts.......2 (Viruses, Spyware and other Nasties)
- function calling problem (C)
- Finding Perfect Numbers. "help plz" (C++)
- "Buffer run detected" (Viruses, Spyware and other Nasties)
- Sorting Algorithms using Time (C++)
- No "Data & Time" Function (Windows 95 / 98 / Me)
- Error message when I run my program in C++ (C++)
- I need special stuff in my project like system("cls") (C++)
Other Threads in the C++ Forum
- Previous Thread: Connect to Network?
- Next Thread: Logical error in finding Palindromes
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






