简体   繁体   English

字符计数程序不输出任何内容

[英]Character counting program doesn't output anything

I've tried Dev-C++ and MinGW(32-bit) but every time i run the code, nothing outputs. 我已经尝试过Dev-C ++和MinGW(32位),但是每次我运行代码时,都没有输出。 I'm using windows 7. below is my code. 我正在使用Windows7。以下是我的代码。

#include<stdio.h>
main()
{
  long nc;
  nc=0;
  while(getchar()!=EOF)
    ++nc;
  printf("Number of chars are %ld ",nc);
}

I've also tried pressing Ctrl-D to signal the EOF but it's not working. 我也尝试按Ctrl-D发出EOF信号,但它不起作用。 when I input " rajkumar " and [enter] it doesn't do anything. 当我输入“ rajkumar”并[输入]时,它什么也没做。 when i input " rajkumar " and ^Z [enter] twice, it ends. 当我两次输入“ rajkumar”和^ Z [enter]时,它结束。 all i'm asking is why it doesn't print the expected output? 我要问的是为什么它不打印预期的输出? and when i tried it first time it worked fine twice, but then i don't know what went wrong. 当我第一次尝试它时,它可以正常工作两次,但是后来我不知道出了什么问题。

Based on this answer , to trigger EOF detection, 基于此答案 ,要触发EOF检测,

  • first do ENTER 首先回车
  • then press control Z (or ^D on Linux), that has to be at the beginning of the line 然后按下控件 Z (或Linux上的^ D),该控件必须在该行的开头
  • press ENTER again if detection was still not triggered 如果仍未触发检测,请再次按ENTER

To signal EOF on Windows, press Ctrl-Z followed by Enter. 要在Windows上发出EOF信号,请按Ctrl-Z,然后按Enter。

Note: this is for the Windows console. 注意:这适用于Windows控制台。 If you're running the program inside your IDE, your IDE may or may not have a way to signal EOF. 如果在IDE中运行程序,则IDE可能会或可能不会发出EOF信号。

getchar() gets a single character and it also accepts Enter. getchar()获得单个字符,并且还接受Enter。 So if you press r and then Enter, its only r that gets entered. 因此,如果先按r再按Enter,则仅输入r。 So you need to press Enter after pressing the keys. 因此,您需要在按键后按Enter。 Without Enter, getchar() won't accept the character. 没有Enter,getchar()将不接受该字符。

Try putting a \\n at the end of your printf. 尝试在打印文件的末尾添加\\ n。 Something like: printf("Number of chars are %ld /n",nc); 类似于: printf("Number of chars are %ld /n",nc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM