Re: Clearing the input buffer - then pausing





"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;
}

.



Relevant Pages

  • [PATCH] Numerous fixes to kernel-doc info in source files.
    ... static inline int ffs ... @buffer: where the data must be copied. ... * struct kfifo with kfree. ... @timer: ...
    (Linux-Kernel)
  • [UNIX] Multiple Vulnerabilities in Citadel/UX
    ... could allow complete control over a vulnerable server. ... Citadel server as can be seen by this simplistic code snippet: ... configuration buffers, leading to the possibility of carrying out a buffer ... int connect_to_host; ...
    (Securiteam)
  • Re: pushing the envelope with sockets
    ... receiving on the socket they are received (upto the buffer size), you can even change what happens if the buffer runs full. ... int read = S.EndReceive; ... class AsyncReader: Reader ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Updated: Reworked Cell OProfile: SPU mutex lock fix
    ... arch-independent pieces of the OProfile kernel driver that this patch ... kernel driver patches. ... into the kernel buffer without holding the buffer_mutex lock. ... int spu_sync_start; ...
    (Linux-Kernel)
  • [PATCH] char: make functions static in synclinkmp.c
    ... int *eof, void *data) ... /* Allocate DMA buffers for the transmit and receive descriptor lists. ... * This buffer is used to pass an assembled frame to the line discipline. ...
    (Linux-Kernel)