Mohamed El Enany
Mohamed El Enany

Reputation: 3

Segmentation fault core dumped error in c

Each time I run this c code I get a segmentation fault (core dumped)...so the array gets printed and everything and in the end of the printing it fives me this segmentation fault...I tried putting a break after the if, the error code does not appear but it only now reads one line and doesn't read multiple

int main(int argc, char* arg[]){
    if (argc != 2){ 
        puts("Error: Usage is caesarcipher <offset>");
        exit(1);
    }
    int i = 0;
    int input;
    char array[100];
    while ((input = getchar())!= '\0'){
        if (input == '\n'){
            array[i] = '\0'; 
            printf("%s\n", array);
            i = 0;
        }else{  
            array[i] = input;
            i += 1;}}
        return 0;
    }
}

Upvotes: 0

Views: 48

Answers (1)

Greg A. Woods
Greg A. Woods

Reputation: 2792

You should check the documentation for getchar(), particularly the return value which indicates an error.

Upvotes: 2

Related Questions