[英]How to exit the character counting program (I'm using dev c++)
I have a problem when runnig the character counting program: 运行字符计数程序时出现问题:
#include<stdio.h>
main()
{
double nc ;
for( nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc) ;
}
I wrote it exactly the same as the code on my textbook(the c programming language 2nd edition by Brian W. Kernighan & Dennis M. Ritchie) and using the dev c++ to edit and compile 我写的代码与我的教科书(Brian W. Kernighan和Dennis M. Ritchie的c编程语言第二版)中的代码完全相同,并且使用dev c ++进行编辑和编译。
When running this program, I found that when I type a string of characters and then press ENTER, it simply shifted to the next line, how am I supposed to do to tell the machine that my input terminates and exit the program and receive the nc ? 运行该程序时,我发现当我键入一个字符串然后按ENTER键时,它只是移到了下一行,我该如何告诉机器我的输入终止并退出程序并接收到nc。 ?
You loop over the string you entered with the keyboard. 您遍历使用键盘输入的字符串。 You will end the loop as soon as a EOF (end of file) character is read.
读取EOF(文件结尾)字符后,您将结束循环。 On Unix, Linux, BSD and so on you can signal a end of file by pressing CONTROL-D.
在Unix,Linux,BSD等上,您可以通过按CONTROL-D来指示文件结束。 On Windows it's CONTROL-Z.
在Windows上是CONTROL-Z。 On Mac it should be CONTROL-D because it's close to BSD Unix.
在Mac上,它应该是CONTROL-D,因为它接近BSD Unix。 (But i'm not sure).
(但我不确定)。
If you need to end the for loop by pressing "ENTER" a test for EOL (end of line) might help. 如果您需要通过按“ ENTER”结束for循环,则对EOL(行尾)的测试可能会有所帮助。
Edited 编辑
for( nc = 0; getchar() != '\n'; ++nc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.