Hello everyone,
i am writing some codes to encrypt text using ASCII codes. but i get segmentation fault at the Encryption menu when validating choice. the coding are simple. am new at C. it is stil at the beginning. stil got a long way to go. can someone tell me where i went wrong and thanks for everyone.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAMESIZE 15
#define TEXTSIZE 250
//Function Prototypes//
void optionLists();
void header();
void encryptMenu();
void decryptMenu();
void existingFile();
void newFile();
//Global Variables//
char *newline = "\n";
char *line = "-------------------------------------------------------------------\n";
char filename[FILENAMESIZE];
int main()
{
header();
optionLists();
printf(line);
printf("Enter Choice: ");
short int choice;
scanf("%d", &choice);
switch (choice)
{
case 0:
{
return 0;
break;
}
case 1:
{
encryptMenu();
break;
}
case 2:
{
decryptMenu();
break;
}
default:
{
printf("Wrong input!\n");
}
}
return 0;
}
void header()
{
printf(line);
printf("MAIN MENU\n");
printf(line);
}
void optionLists()
{
printf("[1] Encryption\n");
printf(newline);
printf("[2] Decryption\n");
printf(newline);
printf("[0] Exit\n");
}
void encryptMenu()
{
printf(newline);
printf(newline);
printf(line);
printf("ENCRYPTION MENU\n");
printf(line);
printf(newline);
printf(newline);
printf("Enter [1] to encrypt existing file\n");
printf(newline);
printf("OR\n");
printf(newline);
printf("Enter [2] to encrypt a new text\n");
printf(line);
printf("Enter choice: ");
short int choice;
scanf("%d", choice);
switch (choice)
{
case 1:
{
existingFile();
break;
}
case 2:
{
newFile();
break;
}
default:
{
printf("Wrong input!\n);");
}
}
}
void decryptMenu()
{
}
void existingFile()
{
printf(newline);
printf(newline);
printf("Enter the current filename:\n");
printf(">");
scanf("%s", filename);
FILE *fp;
fp = fopen(filename, "r");
if (fp)
{
printf("File exists\n");
fclose(fp);
}
else
{
printf(newline);
printf(newline);
printf(line);
printf("File does not exist.\n");
//printf("Program will use", filename, "as new file to encrypt.");
printf(line);
}
}
void newFile()
{
printf(newline);
printf(newline);
printf("Enter your text:\n");
printf(">");
char text[TEXTSIZE];
scanf("%s", text);
int i = 0;
while (text[i] !='\0')
{
text[i] = text[i] + 7;
i++;
}
printf("Your encrypted text is: %s \n", text);
return;
}