To fix this, you need to check that scanf () actually reads the values you need. scanf () returns the number of successfully read items. So, save the return value, check if it is 1. If it is 0, then 0 elements have been read, and you need to clear the stream before trying again. (edit: Well, maybe it’s not necessary to empty it, but at least go past the offensive data in the stream)
As others said, he tries to read an integer, sees 'a' , cannot read an integer, but then 'a' is still in the stream when he tries again.
source share