As originally observed in a comment, for this particular problem, mark it as a duplicate of scanf()
leaves a newline char in buffer. However, it is hard for a novice C programmer to find this solution to their problem — if they realized that the newline was left in the buffer, they probably wouldn't be asking the question in the first place.
There is a collection of other questions that are appropriate targets for duplicates of other aspects of scanf()
. What follows is by no means a definitive list — for example, it doesn't include an entry for "you forgot to put &
in front of simple variables", or "you put &
in front of a (character) array name", or "you must check the return value from scanf()
".
Unfortunately, scanf()
and family are some of the most complex functions out there, and among the hardest to use effectively. They do work for the simple cases if you know what you're doing and nothing goes wrong; they become problematic if you don't know what you're doing or if things go wrong.
It would be nice if there were some other input functions that were more difficult to misuse, but there aren't standard functions for that task. The CS50 course (see questions tagged cs50, the CS50 Stack Exchange site, the CS50 home page at Harvard, and the CS50 GitHub repository) provides a series of functions for single value inputs that are effective and hard to misuse (though there are aspects of their design that can be debated).
One other thing to remember is that the same issues are asked about over and over again because, despite everything, it is not easy to search for certain problems, especially when you don't really understand what's going wrong. On Stack Overflow, there is a continual influx of new programmers. Those who learned last week about the issues with scanf()
will be replaced by a new crew this week. (And C has a particularly cyclical pattern to novice questions, peaking in the autumn and early winter as the newcomers to university courses are starting their exercises.) You simply have to accept that there will be lots of similar questions and get good at diagnosing them. Look at the tag wiki for the c tag too. It's big but has quite a lot of useful information in it.
scanf()
formats in detail you can propose there?scanf()
leaves the newline char in buffer. Or state that it is a duplicate of that. It's the question I use as 'canonical' for that problem. There are a collection of other questions that are canonical duplicates for other aspects ofscanf()
. If you've only been around 14 months or so and are worn out, you need to develop a thicker skin. It doesn't matter how many people you teach this week about the problems withscanf()
(and there are many problems with it); there'll be new people to teach next week.scanf
is the correct answer to this question, in my opinion.fflush(stdin)
. this should be deleted IMHO...fflush(stdin)
. I believe I close-voted that answer before, but it lapsed as no-one else was paying attention.