简体   繁体   English

代码在while循环后不继续

[英]Code doesn't continue after while-loop

Well,好,

everything works well here, except of the last printf I call.除了我调用的最后一个printf之外,这里一切正常。 I want to output the deleted characters in this code:我想在这段代码中输出删除的字符:

#include <stdio.h>

int del_lower_vowels(char c) {
    if(c=='a') {
        return 0;
    }
    if(c=='e') {
        return 0;
    }
    if(c=='i') {
        return 0;
    }
    if(c=='o') {
        return 0;
    }
    if(c=='u') {
        return 0;
    }
    else
        return c;
}

int main (void) {
    printf("Enter a string\n");
    int c;
    int del = 0;
    while((c=getchar()) != EOF)
    {
        c = del_lower_vowels(c);
        if(c==0)
        {
            del +=1;
        }
        putchar(c);
    }
    printf("Deleted characters: %d",del);
    return 0;
}

getchar() is blocking when there's no more input available and you're not redirecting stdin from a file.当没有更多可用输入并且您没有从文件重定向 stdin 时,getchar() 会阻塞。 It will simply wait forever until you either do more input, or send an EOF to the terminal with CTRL+D (Linux) or CTRL+Z (Windows).它会一直等待,直到您进行更多输入,或者使用 CTRL+D (Linux) 或 CTRL+Z (Windows) 向终端发送 EOF。

Just hitting Enter does not close the input stream (standard input in this case), so your program keeps running (which is correct).只是按 Enter 不会关闭输入流(在这种情况下是标准输入),因此您的程序会继续运行(这是正确的)。 When I hit Ctrl+D (this sends EOF), I get the number of deleted characters and the program ends.当我按下 Ctrl+D(这会发送 EOF)时,我会得到已删除字符的数量并且程序结束。

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

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