loop only runs once

Or better still, define the size of your buffer in one place as a constant, and use that throughout.

I can also thoroughly recommend the auto format tool in the IDE for keeping your code legible.