Kevin said:
Let me clarify. I used \n ... ignore did not work, I read the std.
input and I kept getting the character that I pressed plus a character
with ascii value 10 (the enter key). Just in case the \n was giving
problems I figured I'd use the actual ascii value that I wanted to
ignore.
I'm not sure I understand, but it sounds like you ran into a problem
using your input stream. ignore() with a sufficiently large 1st argument
should remove whatever's left of the line, including the '\n' (assuming
that's what you use for the terminator). If that isn't the behavior you
observed then there's a problem with the code or the library. The code
you posted had the ignore() call commented out. It looks like it would
have worked (well, sort of) with that call in place.
I think we're off topic here
Not at all. We're talking about C++, which is the topic of this group.
If you meant that we are straying from your question, well, I never saw
the original post, and it appears from the replies that it was a
platform-specific problem, so technically that's the discussion that
would be off-topic.
Nope, perfectly on-topic.
a is initialized with the std::cin.get so it's something from the
input stream ... you seem to be missing the point ... this is a test
program not something I'm selling.
I'm not missing any point, because I was not addressing any particular
point. I was talking about the code itself, which does, in fact, invoke
undefined behavior due to the error I pointed out. 'a' is obviously NOT
initialized by the std::cin.get, since you are quite clearly testing 'a'
*before* that call occurs. Hence, you are examining the value of a
variable that is indeterminate, and the behavior is undefined.
Your problem has not been handled?
....
OK, fair enough. Let me take another look. Here's the code (with changes
to make it well-defined):
#include <iostream>
int main() {
char a = 0; // initialized
// char nl = (char) 10; // no need for this
std::cout << "Enter: ";
while (a != 'g') {
std::cin.get(a);
std::cout << "a=" << (int)a;
//std::cin.ignore(500, '\n');
}
std::cout << "Gimme Input: ";
std::cin >> a;
return 0; // added (not strictly required)
}
With this, I see output similar to what you reported, and I'm not
surprised. Now, if I uncomment the ignore() call it waits for a line of
input, then gives the character value of the first character in that
line, and waits for another line of input (until the first character is
'g'). This is the expected behavior. You said:
If I uncomment the cin.ignore ... the ignore statement waits for me to
hit enter!
The reason this looks like a bug is that if my cin.get can find a
character on the input stream with ascii code 10 (i.e. the enter key),
then how can it be explained that the cin.ignore cannot find one and
waits for the enter key to be hit?
I don't quite understand this. Neither get() nor ignore() can find the
'enter' value unless you press 'enter'. Presumably, your keyboard input
is something like this: <a><enter><p><enter>. Now, if the ignore() call
is not present, get() finds 'a', 'enter', 'p', 'enter' in that order,
and the program outputs the value for each (four total). With the ignore
call, get() finds 'a', then ignore() removes the 'enter', then get()
finds 'p', then ignore() removes the second 'enter'.
If this doesn't help you understand what is happening, or if you believe
what I'm describing is not what you are observing, then please clarify
what you expected and what you actually observed.
-Kevin