Thread: Beginner - User Validation & encryption

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    Beginner - User Validation & encryption

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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf wants a pointer, not a value. (You are missing the & in front of the word choice.)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    wow, how did i miss that. thank you.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    hi everyone. the program is rather big for me to code but am trying my best. the program does a lot of things from counting words finding longest word and so on. i am tackling each problem separately in a function. so my problem for now is that i do not know how to display the longest word that is stored in a text file and display the number of characters the longest word contains. is there someone here willing to help and guide me where to start. thanks.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner C Question Help
    By pmacdonald in forum C Programming
    Replies: 32
    Last Post: 06-22-2011, 09:32 PM
  2. Replies: 0
    Last Post: 09-26-2009, 12:33 PM
  3. Maz's calendar. [IRC user data storage planning]
    By Maz in forum C Programming
    Replies: 1
    Last Post: 08-18-2009, 12:00 PM
  4. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM

Tags for this Thread