#sscanf behaiving weirdly

15 messages · Page 1 of 1 (latest)

neon blade
#
while (fgets(buffer, sizeof buffer, stdin) == NULL || sscanf(buffer, "%d %d %d %d %d", &layerCount, &topLayerHeight, &layerHeightGrowth, &trunkWidth, &trunkHeight) != 5)
{
    printf("Invalid input. Reinput all 5 values.\n");
}

Why does this piece of code freeze when i input "1 2 3 4 ^Z"? fgets should read "1 2 3 4 " and then sscanf would fail, and thus printing "Invalid input. Reinput all 5 values.\n"

fathom girderBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

dawn mango
#

Should that be && instead of ||?

#

(incidentally, this is C code which ideally should be posted in #1013104018739974194 )

neon blade
neon blade
#

I seemed to have found the cause. It seems that in windows, when I type a ^Z, if it's not the only character in the line, then it's not treated specially as EOF but as a regular character. When that happens, all characters after the ^Z is ignored in this line

#

So when i input "1 2 3 4 ^Z", since all characters after ^Z was ignored, there would be no newline, and that caused fgets to block

dawn mango
#

Was about to add that I cannot test it the same way, because on Linux CTRL-Z just sends the application to the background.

#

Oh, and you're right about using ||.
Ignore my comment.

honest shore
#

sorry to be that guy, but why are you bothering with sscanf and stuff? for C, there's a whole separate section down there…

neon blade
#

!solved

fathom girderBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] sscanf behaiving weirdly