This has always served me well.
Though in your case, perhapsCode:ignore((unsigned)-1, '\n');
Would make the most sense.Code:ignore(MAX_PASSWORD_LENGTH, '\n');
This has always served me well.
Though in your case, perhapsCode:ignore((unsigned)-1, '\n');
Would make the most sense.Code:ignore(MAX_PASSWORD_LENGTH, '\n');
master5001, the problem with that approach is that allows ONLY 16 characters, not up to and including 16 characters.
If the string ends with a \n, there's no new input so don't do anything.
If the string doesn't end with an \n, there's something still in the buffer.
Note you have to do this before stripping off the \n to compare with your password. (I would say, if there's more input in the stream, the input is automatically wrong, ignore and continue; otherwise check as above.)
>> master5001, the problem with that approach is that allows ONLY 16 characters, not up to and including 16 characters.
The point is, even if you don't care if there are a total of 1,342,312,678,793,163 '\n' characters put into the text box, you can only possibly see so many at one given time. Thus there is no need to use MAX_INT or (unsigned)-1 or any other exceedingly large numeric value. MAX_CHAR is already more than the buffer can hold. Thus cin won't even need to look past MAX_PASSWORD_LENGTH + 1.Code:ignore(MAX_PASSWORD_LENGTH+1, '\n');
tabstop, that is incredibly obnoxious. Is that really the lengths that you need to go to so that you can accept input safely from the console? Every single time input is required? No wonder there are so many buffer overflows in C/C++ code, if it's that hard just to do something like accept input safely. There really needs to be a smart function that handles that for the programmer.
It is not that big of a deal, maxhavoc. If you are using cin.get() like tabstop has up there you are simply using MAX_PASSWORD_LENGTH instead of your magic number. And how else would you have someone know when to stop reading?
As I also mentioned before, you can also use the std::getline() function in conjunction with std::string. Which would entirely circumvent the parts which you seem to have the hardest time grasping: the idea of a fixed size buffer.