I was working through some example programs in the C++ book I'm reading, and decided they are mostly crap because, while they address the topic in the chapter, they do so in a manner that is prone to error.
Author wrote:
1 2 3 4 5 6 7
|
char ch;
cin >> ch;
while(ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
{
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
}
|
Better, I thought, is this:
1 2 3 4 5 6 7 8
|
char ch;
cin.get(ch);
while(ch != 'a' && ch != 'b' && ch != 'c' && ch != 'd')
{
if (ch=='\n')
cout << "Invalid input. Please enter a, b, c, or d.";
cin.get(ch)
}
|
This way I'm only outputting a single line if the user types in something like "gflkgjflkgtfsg" at the cin prompt, and will process a "correct" entry no matter where it appears in the cin buffer. So if "twerlkjtwretbrewlkjrtw" is their input, the "b" will get processed. However, this leaves the cin buffer with a lot of potential garbage.
What I really want is this:
1 2 3 4 5 6 7
|
cin.get(ch)
while(ch != 'a' && ch != 'b' && ch != 'c' && ch != 'd')
{
cout << "Invalid input. Please enter a, b, c, or d.";
cin.ignore(rest of cin buffer); // dump buffer
cin.get(ch); // new prompt
}
|
The trouble is that cin.ignore() requires a length (and potentially a delimiter) but I have no idea what that length is. What IS the length of cin?
If I do something like this:
cin.ignore(1024, '\n')
that will work fine for most keyboard input, but what if the cin buffer contains a redirection from a file? Then, there can be many instances of '\n' (well beyond 1024 characters) but still a ton of junk in the cin buffer. It doesn't make sense to check for EOF because if the junk input is from the keyboard, EOF probably won't exist. Further, to supply EOF as a delimiter, I still need to know LENGTH, since a redirected file can be millions of bytes long.
I know I'm over-thinking CIN, but I'm really curious how this has been dealt with traditionally. I suspect there are methods to handle this, but I'm only on chapter 6 of C++; my set of tools to resolve this puzzle is very limited.