scanf not working as expected

After first scanf(), in every scanf(), in formatting part, put a whitespace

So change this

scanf("%c",&val);

into this

scanf(" %c",&val);

Reason is, scanf() returns when it sees a newline, and when first scanf() runs, you type input and hit enter. scanf() consumes your input but not remaining newline, so, following scanf() consumes this remaining newline.

Putting a whitespace in formatting part makes that remaining newline consumed.

Leave a Comment