提问人:TaxFrog 提问时间:7/15/2022 最后编辑:TaxFrog 更新时间:7/16/2022 访问量:184
while ((c = getchar()) != EOF) 即使代码与 book 几乎相同,也不会终止
while ((c = getchar()) != EOF) not terminating even though code near identical to book
问:
Kernighan 和 Ritchie 的 C 语言练习 1-8
void countBlanksTabsNewlines() {
int c, newLines, tabs, blanks;
newLines = 0;
tabs = 0;
blanks = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++newLines;
} else if(c == '\t'){
++tabs;
} else if(c == ' '){
++blanks;
}
}
printf("New Lines = %d , Tabs = %d , Blanks == %d", newLines, tabs, blanks);
}
当我使用任何输入然后按回车键时,while 循环不会终止。 我的代码基于仅计算书中新行的示例,所以不确定如何解决。 谢谢:)
编辑:我没有提到我正在使用 CLion IDE,在尝试了 CLion 中建议的快捷方式(未成功)后,我尝试在在线编译器中执行相同的操作,并且 ctrl+D 有效。
答:
-2赞
RickSanchez
7/15/2022
#1
要获取 EOF,您需要在控制台输入字符串末尾的 Windows platfrom 上按 Ctrl+z。
评论
0赞
0___________
7/15/2022
从其他用户的评论中复制?
2赞
Weather Vane
7/15/2022
在 Windows 上,您需要在行输入的开头按 Ctrl-Z - 这是行上唯一的字符。
评论