 Let's run this program that asks for a number of units purchased and the unit price, and then prints the total price of the order. There's nothing particularly new here. But what happens if we run the program again, and this time accidentally entered the price first? This time the program didn't ask for the price and went ahead and did the calculation. What's going on here? The answer is that scanf works with something called the input buffer. The first scanf wants to read an integer from the input buffer, but it's empty. Scanf will wait for us to type something, the digits one, two, and the enter key. That puts the digits one, two, and a new line character into the input buffer. When looking for a number scanf starts at the current position in the input buffer and skips over any white space, in this case there is none, and keeps gathering items from the buffer as long as they could possibly be part of an integer. It stops when it gets to the new line, because that's not a digit. The digits one and two are transformed into a 12 and stored in the units variable. The next prompt appears on the screen and we have another scanf for the unit price. Scanf starts at the current position in the buffer and, because it's looking for a number, skips over the white space. The input buffer is empty once again. Scanf waits for us to type something and puts that into the input buffer. Starting at its current position, it gathers up everything that could possibly be part of a double, stopping when it gets to the new line, converts that to the numeric value 3.57 and puts it into unit price. There's still a new line in the input buffer, but we're not doing any more reading, so it's not a problem. Now consider what happens when we accidentally type the 3.57 first. Once again, the input buffer is empty when the program encounters the first scanf. We type the 3.57 and press enter, putting 3, a period, 5, 7 and new line into the input buffer. Scanf skips white space, if any, and gathers up everything that could possibly be an integer, and stops at the decimal point, which cannot be part of an integer. The 3 is converted to an integer and stored in the units variable. The program then proceeds to the scanf looking for the unit price. There's still some data in the input buffer, and scanf does find the decimal point, the 5 and the 7, stopping at the new line character, which can't be part of a number. It stores the .57 into unit price and proceeds to do the calculation without waiting for you to type anything. A very unpleasant surprise indeed. This really becomes a problem when trying to read a single character. In this code, let's say you type in a rating of 6 at the first prompt. That puts 6 and new line into the input buffer. The 6 gets stored in the rating, and now the second scanf looks for a character, any character at all, and there's the new line, which qualifies as a character, and that goes into the answer variable. What we need is some way to clear out the input buffer. If we can do that, when the program comes to the next scanf, the input buffer will be empty and the program will wait for our input. Here's the program, without any code for clearing the input buffer. When I type 6 and press the Enter key, that leftover new line causes a problem. To clear the input buffer, I'm going to use the get char function, which gets the next character from the input buffer and returns it to us. I'm going to call get char in a while loop. As long as the next character in the input buffer is not equal to the new line character, I'm going to keep going. There's nothing in the body of the loop. We're using this loop to discard characters from the input buffer. Whenever you have an empty loop body, it's a good idea to put in a comment to let the people who are reading your code know why that loop is an empty one. When get char finally does consume the new line character from the input buffer, this condition will be false and the loop will finish. Let's rebuild and run. And this time, if I put an N or a Y, everything is working great. If your program does a lot of input, you may need to clear the input buffer many times in your code. Instead of copying and pasting this while loop, you'll put the while loop into a function to make your code shorter and more readable. We'll talk about functions much more in the following videos. The most important thing to know here is how scanf interacts with the input buffer and how you sometimes have to clear out that input buffer to make the program work the way you want it to.