能便宜 运行窗口会出现“segmentation fault”  求助啊~~~~

#include <stdio.h>
#include <math.h>
char encode(char plain,int key);
char decode(char cipher,int key);
int main(void){
    FILE *inp,*outp;
    char p;
     int key,a;
printf("enter the key");
scanf("%d",&key);
printf("choose");
scanf("%d",a);
   if(a==1){
       inp=fopen("plaintext.txt","r");
       outp=fopen("ciphertext.txt","w");
        while((p=fgetc(inp))!=EOF)
               {
                fputc(encode(p,key),outp);
                printf("%c",p);
                fclose(inp);
                fclose(outp);
          }}
    else{
        inp=fopen("ciphertext.txt","r");
        outp=fopen("meassage.txt","w");
         while((p=fgetc(inp))!=EOF)
               {
                fputc(decode(p,key),outp);printf("%c",p);
                }
   fclose(inp);
   fclose(outp);}
return(0);
}


char encode(char plain,int key)
    {
    char result;
     result=(plain-65+key%26)%26+65;
     return(result);}
char decode(char cipher,int key)
     {char result;
      if(cipher-65>=(key%26)){result=(cipher-65+26-key%26)%26+65;}
      else{result=cipher+26-key%26;}
      return(result);}