统计输入字符数小程序,哪里出了有关问题

统计输入字符数小程序,哪里出了问题?本帖最后由 limuyuan2008 于 2013-07-06 18:07:39 编辑#include stdi

统计输入字符数小程序,哪里出了问题?
本帖最后由 limuyuan2008 于 2013-07-06 18:07:39 编辑

#include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
nc = nc + 1;
printf("ld\n",nc);
}



运行之后,不管输入什么还是直接按回车都没有反应

顺便问一下,到底是字母L+D还是数字1+D?

谢谢啦。。

书上还提供了另一个版本,结果也是一样的
#include <stdio.h>
main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n",nc);
}

[解决办法]
L+D  

vc++中用按回车之后,按Ctrl+Z,再回车 结束输入
[解决办法]


windows中的命令行程序是按Ctrl+Z,然后回车输入EOF
Unix/Linux中是按Ctrl+D
[解决办法]
char str[80];
scanf("%s", str);
printf("%d\n", strlen(str));

[解决办法]
#include <stdio.h> 
main() {     
    int nc;     
    nc = 0;     
    while (getchar() != 0x0a)         
          nc = nc + 1;     
    printf("%ld\n",nc); 
    getch();
    } 


判断条件是否为换行,若是换行则停止循环。
后加的getch()是为了能够看清屏幕显示的字符个数。
[解决办法]
引用:
Quote: 引用:

#include <stdio.h> 
main() {     
    int nc;     
    nc = 0;     
    while (getchar() != 0x0a)         


          nc = nc + 1;     
    printf("%ld\n",nc); 
    getch();
    } 



判断条件是否为换行,若是换行则停止循环。
后加的getch()是为了能够看清屏幕显示的字符个数。

直接报错:[Error] D:\My Documents\C-Free\Temp\未命名4.cpp:8: error: `getch' was not declared in this scope

最后是getchar();
[解决办法]
输入
a、b、c、回车、Ctrl+Z、回车
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

#include <stdio.h> 
main() {     
    int nc;     
    nc = 0;     
    while (getchar() != 0x0a)         
          nc = nc + 1;     
    printf("%ld\n",nc); 
    getch();
    } 


判断条件是否为换行,若是换行则停止循环。
后加的getch()是为了能够看清屏幕显示的字符个数。

直接报错:[Error] D:\My Documents\C-Free\Temp\未命名4.cpp:8: error: `getch' was not declared in this scope

最后是getchar();

还不如去掉或者直接加return 0了。。
还有,0x0a是什么意思?换行符?

0x0a是\n的ASCII码(16进制)