而((C =的getchar())!= EOF)未终止 [英] while ((c = getchar()) != EOF) Not terminating

查看:319
本文介绍了而((C =的getchar())!= EOF)未终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在读的C语言程序设计,我得到了输入和输出的这一部分。

我读过的其他线程说,控制台无法识别输入的 EOF 。所以,我应该使用<大骨节病> CTRL + <大骨节病>以Z 在Windows或<大骨节病> CTRL + <大骨节病> D 在Unix中(没有那些工作对我来说)。

我也看到其他人问同样的说,他们可以使它的工作,在他们的codeS的问题是语法不是程序不终止。

有另一种解决方案?

这是code:

 的#include&LT;&stdio.h中GT;
主要()
{
    INT NB,NL,NT,​​C程序;
    NB = 0;
    NL = 0;
    NT = 0;
    而((C =的getchar())!='\\ n'){
        如果(C =='')
            ++ NB;
        否则,如果(C =='\\ n')
            ++ NL;
        否则,如果(C =='\\ t')
            ++新台币;
    }
    的printf(输入有%d空白,%d个标签,而%d换行\\ n,NB,NT,NL);
}

编辑: \\ n 应该是一个EOF,我乱搞之前,我贴我忘了我改成了:P

它不工作EOF没有,我只是跳过一个。谢谢你。


解决方案

 ,而((C =的getchar())!='\\ n'){
    如果(C =='')
        ++ NB;
    否则,如果(C =='\\ n')
        ++ NL;
    否则,如果(C =='\\ t')
        ++新台币;
}

据while循环的情况下,你会不会当用户输入一个新的生产线能算的新行数,因为你会停止,而循环字符('\\ n')

除此之外,计数空格和制表符只是正常工作。

<大骨节病> CTRL + <大骨节病>以Z 将被视为Windows中的EOF。但是,为了识别它在你的程序中,while循环使用条件((C =的getchar())!= EOF)
现在,当用户presses组合键:<大骨节病> CTRL + <大骨节病>以Z ,它会输入到控制台EOF,程序应该认识到它作为一个字符输入。

这样做可以让你在计数输入线数

所以,我的建议是:

 ,而((C =的getchar())!= EOF){
    如果(C =='')
        ++ NB;
    否则,如果(C =='\\ n')
        ++ NL;
    否则,如果(C =='\\ t')
        ++新台币;
}

So I've been reading "The C Programming Language" and I got to this part of inputs and outputs.

I've read other threads saying that the console doesn't recognize enter as EOF. So that I should use CTRL + Z in Windows or CTRL + D in Unix (neither of those is working for me).

I also read other people asking the same saying they could make it work, the problem in their codes was syntax not the program not terminating.

Is there another solution?

This is the code:

#include <stdio.h>
main()
{
    int nb, nl, nt, c;
    nb = 0;
    nl = 0;
    nt = 0;
    while ((c = getchar()) != '\n'){
        if (c == ' ')
            ++nb;
        else if (c == '\n')
            ++nl;
        else if (c == '\t')
            ++nt;
    }
    printf("Input has %d blanks, %d tabs, and %d newlines\n", nb, nt, nl);
}

Edit: The \n was supposed to be an EOF, I was messing around before I posted and I forgot I changed it :P

It doesn't work with EOF neither, I just skipped that one. Thanks.

解决方案

while ((c = getchar()) != '\n'){
    if (c == ' ')
        ++nb;
    else if (c == '\n')
        ++nl;
    else if (c == '\t')
        ++nt;
} 

According to the condition in your while loop, you will not be able to count number of new lines because you will stop the while loop when the user inputs a new line character ('\n')

Other than that, counting blanks and tabs just works fine.

CTRL+Z will be recognized as a EOF in windows. But in order to recognize it in your program, use condition in while loop as ((c = getchar()) != EOF). Now when the user presses the key combination: CTRL+Z, It will input to console as EOF, and the program should recognize it as a character input.

Doing this will allow you to count number of lines in the input

So, my suggestion is:

while ((c = getchar()) != EOF){
    if (c == ' ')
        ++nb;
    else if (c == '\n')
        ++nl;
    else if (c == '\t')
        ++nt;
} 

这篇关于而((C =的getchar())!= EOF)未终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆