: You know what... I just commented out all prior cin's and using ONLY
: cin.get() worked great. Does cin always retain the very last thing
: the user inputs? I guess that would make sense but I assumed
: initially that cin dumped the user's input into the variable and
: automatically "emptied" itself but I guess not... so then, I guess
: cin.get() is essentially saying "I want you to get input from the
: user and if the user has already given me some input somewhere else,
: just use that," thus, cin.get() appears as though it's being skipped
: unless you clean cin beforehand so that it waits for a user's
: input... is that correct? Why do so many people just assume a
: beginner knows to clear cin or whatever is necessary and just say
: "use cin.get() and you'll be fine," then get upset with the beginner
: when they say "cin.get() didn't work for me?" Anyway, I'm just
: thinking aloud with that last sentence.
I heard you
std::cin doesnt have an internal buffer--it retrieves its input from
stdin. So, if stdin isnt flushed (cleared), then cin will indeed
return whatever value was left in the buffer.
Some routines do this automatically (Such as cin::operator>> () )
To flush it manually, just use fflush (stdin)
: What about #3, though? What kind of issues could that potentially
: cause if used or would it be fine to use that method in the context
: of solely making a user hit a key before a program exits?
There is nothing wrong with #3 besides that it is nonportable.
It gets input directly, hence doesnt need to read from stdin.
Im not sure if the input read is stored in stdin though. I dont think so.
: I apologize if this post is annoying in any way - I'm just trying to
: get a solid grasp of this stuff before I continue on. Thanks guys.
: By the way, I'm Deitel's "How to Program C++: 4th Edition" which
: seems to be great so far and I'm sure the answers to the questions
: I'm asking in this thread are apparent in the book but I just tend
: to venture off into other things and attempt things through trial
: and error. It also helps me to get a feel for what you seasoned
: programers do, what good coding practices are, etc.
: Thanks again, y'all.
I personally prefer using getchar() when terminating:
cout << "Thanks for playing!" << endl;
int c = getchar();