你再次看到的问题是,它确实是看一看一面的,但它只是你所期望的特性。 扫描的确是格式化的投入。 你第一次说了,你再次告诉我们,我们会期望一些。 但是,你们真正进入了不止几个方面:
Number?
1234.5678<enter>
When you press the enter key, it is actually inserting a character into your input stream. As you may know, we use
to represent newline, the character you get when you press enter. So your input stream actually looks like "1234.5678
".
So scanf does its thing and reads 1234.5678 and then it sees
. It says "oh, that s not part of the number, so I ll stop." Well, your input still has the
. The next time you call scanf, you tell it to read a character. The user types whatever they want, but that goes behind the
from the previous scanf. So scanf tries to match the input stream with a character and says "ok, the first thing in this input stream is a character, and it s
, so I ll return that." The stuff the user typed is still sitting in the input stream.
So a simple way to get rid of it is to have a loop that empties all remaining characters from the input stream until it finds
.
printf("Number?
");
scanf("%f", &number1);
while( getchar() !=
);
在这次空闲活动之后,你的投入流将是空洞的。 因此,在你称之为扫描时,它会等到用户打造某种东西,并将使用用户分类的任何东西。
还指出,扫描仪有收益价值,在打电话后应加以核对。 阅读扫描,看看它返回的情况。