简体   繁体   中英

Code doesn't continue after while-loop

Well,

everything works well here, except of the last printf I call. 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. 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).

Just hitting Enter does not close the input stream (standard input in this case), so your program keeps running (which is correct). When I hit Ctrl+D (this sends EOF), I get the number of deleted characters and the program ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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