Re: Clearing the input buffer - then pausing
- From: Ray Mitchell <RayMitchell_NOSPAM_@xxxxxxxxxxxxxxxxxx>
- Date: Sat, 9 Apr 2005 14:57:03 -0700
"Frank Hickman [MVP]" wrote:
> "Ray Mitchell" <RayMitchell_NOSPAM_@xxxxxxxxxxxxxxxxxx> wrote in message
> news:64F0E2AE-5B41-49E7-B015-8A3283CA803C@xxxxxxxxxxxxxxxx
> > VS .NET 2003: I should know this, but I don't! I would like to pause my
> > program at some arbitrary point and resume only when the Enter key is
> > pressed
> > once (preceeded by zero or more other characters). The code below works
> > if I
> > do not comment out the first cin line. If I do, however, it takes two
> > strokes of the Enter key. What appears to be happening is that the
> > ignore
> > method does not operate on a new buffer, but that's just a guess. I've
> > tried
> > several different things but it has become painfully clear that I should
> > consider another line of work! Additionally, it seems there must be less
> > complex way to accomplish this task. Thanks.
> >
> > Note: cin.ignore(INT_MAX, '\n');
> > It seemed to me that cin.ignore(INT_MAX); would be more appropriate, but
> > it
> > is worse.
> >
> > #include <iostream>
> > #include <climits>
> > using namespace std;
> >
> > int main()
> > {
> > int value = 0;
> > cout << "Enter an intergal value: ";
> > cin >> value; // Requires 2nd Enter if this line commented out
> > cout << "The value was " << value << endl;
> >
> > int ch;
> > cin.ignore(INT_MAX, '\n'); // It seemed that cin.ignore(INT_MAX); would
> > be
> > while ((ch = cin.peek()) != '\n' && ch != EOF)
> > ch = cin.get();
> >
> > return 0;
> > }
>
>
> I do this in some of my console apps...
>
> #include <conio.h>
>
> int ch= 0;
> // Ignore all keys except the Enter key...
> while( ch != 0x0d )
> {
> if ( (ch= getch()) == 0xe0 || ch == 0x00 )
> { // Function key or some other special key was pressed, ignore it...
> getch();
> }
> }
>
> --
> ============
> Frank Hickman
> Microsoft MVP
> NobleSoft, Inc.
> ============
> Replace the _nosp@m_ with @ to reply.
>
>
>
Hi Frank,
Thanks for the response. Yes, I've done that type of thing but I'm
wondering about something that is completely ANSI compatible, not
OS-dependent. I believe something like a flush of the cin buffer followed by
a cin.get() might be more general, but I've not yet figured out how to do it
consistently. Additionally, in C at least (I'm much more familiar with C
than C++), I believe that even fflush() is not portable since the ANSI
standard (C89) only defines the flushing of an output buffer, not an input
buffer. I've got the same issue with a similar C program, where I've tried
the following, with about the same results as with the C++ program I listed
the code for previously:
#include <stdio.h>
int main(void)
{
int value = 0;
printf("Enter an intergal value: ");
scanf("%d", &value); // Commenting out gives different "pause" results
printf("The value was %d\n", value);
scanf("%*[^\n]%*c");
getchar();
return 0;
}
.
- References:
- Clearing the input buffer - then pausing
- From: Ray Mitchell
- Re: Clearing the input buffer - then pausing
- From: Frank Hickman [MVP]
- Clearing the input buffer - then pausing
- Prev by Date: Re: HELP! where is windows.h ??
- Next by Date: recreate a thread
- Previous by thread: Re: Clearing the input buffer - then pausing
- Next by thread: Re: Clearing the input buffer - then pausing
- Index(es):
Relevant Pages
|