-
getch() twice??
im writing a simple console program and i want this simple console program to stop at certain points and wait for the user to hit enter.
this simple task is accomplished by me putting the following:
Code:
cout << "Press enter to continue...";
getch();
the only problem is, the code wont stop there. but if i put getch() in there twice, like so:
Code:
cout << "Press enter to continue...";
getch();
getch();
it works. whats going on here, and how do i get getch() to work like its supposed to?
-
You probably have some input left in the buffer from an ealier cin or something similar.
-
How it's supposed to work is undefined. You've probably got a stray '\n' in a buffer somewhere that is being eaten up by the first getch(). Perhaps if you state your compiler/os some-one may be able to give you another non-standard way of doing the same thing that doesn't look like a kludge.
-
ok, i guess now would be a good time to ask:
what is a buffer and how do i keep input from being left in it? *blush*
also, im using Dev C++
-
A buffer is a region in memory that can be used in a similar way to a shock absorber. To even out frequent i/o operations in your case. You can empty a buffer by calling a function that removes i/o that has been stored there (as you have done with the double getch()).
However, without seeing the code that is filling your particular buffer, it's hard to suggest an alternative for emptying it (I was under the impression that most implementations of getch() didn't use buffered i/o anyway). However if you don't mind the characters being echoed on the screen you could try cin.get().
-
If the getch thing doesn't work, you could always, (and only if you NEED to) use system("pause"); it's good, but a tad slow.:D
-
Slow? :D
Hehe. Who cares if it's a bit slow, when the whole purpose is to pause and wait for the user?
-
getche() will also "echo print" the input.
The trick is to add cin.ignore(x, '\n') or cin.ignore(x, '\0') following getch(), where 'x' represents an arbitrarily significant number of chars meant to completely flush the buffer, i.e. 10, 20, 100.
All that being said, don't use getch() or getche(). They're both non-standard and non-portable. cin.get() is both standard and, to the best of my knowledge, portable.
The potential problem with cin.get() is that get() reads up to - keeping this very specific to the topic - '\n', for example, but leaves it in the stream. get() doesn't consume the newline character the way that getline() does (for those taking notes).
One way to skirt the portability issue (suggested by Prelude) is to create a function such as 'pause();'. Simply define the function using suitable code, even a system call, that a user can easily change to accomodate his/her environment:
Code:
// my methodology here
#include <cstdlib>
inline void pause() { system("PAUSE"); };
int main(void)
{
// code
pause();
return 0;
}
Here 'pause();' can be used anywhere in the code and can be changed at the function declaration/defintion without messing with anything else in the program. (Documentation would be a very nice touch, by the way.) :)
Note that on a Windows O/S, you're going to get that "Press any key to continue..." message on a 'system' pause, which may not be acceptable for all applications.
-Skipper
P.S. Agreed with Sang-drax. We're stopping the program here. :D