簡體   English   中英

代碼在while循環后不繼續

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

好,

除了我調用的最后一個printf之外,這里一切正常。 我想在這段代碼中輸出刪除的字符:

#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;
}

當沒有更多可用輸入並且您沒有從文件重定向 stdin 時,getchar() 會阻塞。 它會一直等待,直到您進行更多輸入,或者使用 CTRL+D (Linux) 或 CTRL+Z (Windows) 向終端發送 EOF。

只是按 Enter 不會關閉輸入流(在這種情況下是標准輸入),因此您的程序會繼續運行(這是正確的)。 當我按下 Ctrl+D(這會發送 EOF)時,我會得到已刪除字符的數量並且程序結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM