[问题] 新手请教scanf用法

楼主: rexrexchian (rexrex)   2017-12-12 16:41:13
开发平台(Platform): (Ex: Win10, Linux, ...)
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
C
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
假设输入一句字串,而只取出想要的数值
喂入的资料(Input):
比如输入 A is 5 years old 则取出 A 跟 5 两个值
预期的正确结果(Expected Output):
错误结果(Wrong Output):
程式码(Code):(请善用置底文网页, 记得排版)
scanf("%c is %d years old", &a, &years);
补充说明(Supplement):
目前这样写是正确的,但不太清楚为什么这样写OK
是说scanf如果遇到一样的字元会自动忽略吗(比如说...is...years old)
还有写题目时会遇到要用到 scanf("") != EOF
电脑是怎么判断什么时候会终止的?
比如说测资是
A is 5 years old
B is 8 years old
C is 17 years old
B is older than A
C is older than A
scanf怎么不会把第四句跟第五句读取进去?
希望版上的强人能够提点一下...
先谢谢底下回复的版友
作者: MOONRAKER (㊣牛鹤鳗毛人)   2017-12-12 17:28:00
你要输入EOF他才会知道有EOF整个档案输入最后会有EOF 不是档案的话你要自己按可能是CTRL-D (*nix) 或CTRL-Z (DOS)
作者: jerryh001   2017-12-12 17:31:00
手动按ctrl+z 或是读档结尾 就是EOF

Links booklink

Contact Us: admin [ a t ] ucptt.com