作者rexrexchian (rexrex)
看板C_and_CPP
标题[问题] 新手请教scanf用法
时间Tue Dec 12 16:41:13 2017
开发平台(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怎麽不会把第四句跟第五句读取进去?
希望版上的强人能够提点一下...
先谢谢底下回覆的版友
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.119.121.6
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1513068076.A.A1C.html
1F:→ MOONRAKER: 你要输入EOF他才会知道有EOF 12/12 17:28
2F:→ MOONRAKER: 整个档案输入最後会有EOF 不是档案的话你要自己按 12/12 17:29
3F:→ MOONRAKER: 可能是CTRL-D (*nix) 或CTRL-Z (DOS) 12/12 17:30
4F:推 jerryh001: 手动按ctrl+z 或是读档结尾 就是EOF 12/12 17:31