您可从标准投入中读到,有2项功能:getchar()
和scanf(
。 你们需要了解他们的工作方式。
getchar()
is easy: it returns the next available character in the input stream (or waits for one or returns EOF
)
scanf("%d", ...)
is more complex: first, it optionally discards whitespace (spaces, enters, tabs, ...), then it reads as many characters as possible to represent an integer, and stops at the first character that can t be used for integers, like a
.
由于你在座右翼上座,您的<代码>getchar(>)的电话将具有阻止scanf(>
的特性,下个scanf(<>>
将从该密码上传。
如果您的投入如<代码>q1w22e333r44>(>/code = 4),则您的方案将发挥作用。
如果你们的意见是这样的话
q 1
w 22
e 333
r 4444
after the first time through the loop (where charray[0]
gets q
and inarray[0]
gets 1
), the getchar()
will get
leaving the w
"ready" for scanf, which of course fails ... and is then "caught" by the next getchar()
; and the "22"
gets assigned in the 3rd time through the loop (to inarray[2]
).
因此,你需要审查你的法典。
另外,<代码>scanf() 回归数值。 <>可见>
if (scanf("%d", &inarray[i]) != 1) /* error */;