Tuesday, April 21, 2009

How To #1 :: Flushing input stream in C++

As I was telling in my last post (CrackMe #1), the solution by DoomsDay forced me to re-check my code for errors. I had not expected negative numbers to be valid secret codes. I had implemented the following method to reject the negative numbers from Test #1 itself. See the code below :
ReEnterCode:
XXXXInKey = getchar();
XXXXwhile( InKey != '\n' )
XXXX{
XXXXXXXXif( ( InKey >= '0' ) && ( InKey <= '9' ) )
XXXXXXXX{
XXXXXXXXXXXX// Key gets accepted and stored
XXXXXXXX}
XXXXXXXXelse
XXXXXXXXXXXXgoto ReEnterCode;

XXXXXXXInKey = getchar();XXXXXXXX// Get the next key from user
XXXX}
XXXXprintf("Test #1 : PASSED");


At first sight of the code, everything seems OK. Gets a char from user, checks if it's a digit. If it is, accepts it; else prompts user for Re-Input. But, this code fails.

Lets see why. Let's analyze for input -8.
Program receives '-'. Does not accept it. Goes to the label ReEnterCode.
There, it gets the next instruction : InKey = getchar();
And what's the next char ? '8'.
So, it continues execution with '8', which gets accepted later.

The problem occurs because the input stream is not flushed before jumping to ReEnterCode. It can be solved this way :
ReEnterCode:
XXXXInKey = getchar();
XXXXwhile( InKey != '\n' )
XXXX{
XXXXXXXXif( ( InKey >= '0' ) && ( InKey <= '9' ) )
XXXXXXXX{
XXXXXXXXXXXX// Key gets accepted and stored
XXXXXXXX}

XXXXXXXXelse
xxxxxxxx{
xxxxxxxxxxxxwhile( ( (InKey = getchar()) != '\n' ) || InKey != EOF );
XXXXXXXXXXXXgoto ReEnterCode;
xxxxxxxx}

XXXXXXXInKey = getchar();XXXXXXXX// Get the next key from user
XXXX}
XXXXprintf("Test #1 : PASSED");


I uploaded Ver.2.00 of CrackMe #1 with this problem corrected.

No comments:

Post a Comment

I would be happy to hear from you.