Hi, Im trying to make a caesar cipher, but a windows error comes up when running. Where have I gone wrong??
Sam

/////////////////////////////////////////////////
///                                          ///
///      NAME: SAM BERWICK                   ///
///      DATE: 18/11/2010                    ///
///      PROJECT: CAESAR CIPHER              ///
///                                          ///
////////////////////////////////////////////////

#include <stdio.h>    /// Stand In/Output
#include <string.h>   /// strlen()
#include <ctype.h>    /// toupper()
#include <time.h>     /// rand()

///GLOBAL VARIABLES////////////////////////////
char alpha[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
                 'S','T','U','V','W','X','Y','Z'};
int  nalpha[] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
////////////////////////////////////////////////

///FUNCTION PROTOTYPES//////////////////////////
//void encrypt_txt(); // used to encrypt a text file
void encrypt_input(); // used to encrypt a text file
//void unencrypt_txt(); // used to unencrypt
//void unencrypt_input(); // used to unencrypt
////////////////////////////////////////////////

int main()
{
    int  mainmenu=0;
    int  num;
    int  i1=0;

    printf("Please Enter 1 - To Encrypt a text file\n");
    printf("Please Enter 2 - To Encrypt text which you will enter\n");
    printf("Please Enter 3 - To Unencrypt a text file\n");
    printf("Please Enter 4 - To Unencrypt text which you will enter\n");
    printf("Please Enter 5 - To Quit\n");
    while(i1==0)
    {
        fflush(stdin);
        scanf("%1d",&mainmenu);
        switch (mainmenu)
        {
           case 1:
                i1=1;
                printf("place holder\n");
                break;
           case 2:
                i1=1;
                encrypt_input();
                break;
           case 3:
                i1=1;
                printf("place holder\n");
                break;
           case 4:
                i1=1;
                printf("place holder\n");
                break;
           case 5:
                return(1);
           default:
                printf("Incorrect Input Please Enter Again\n");
                break;
        }
    }
           
    system("pause");
}

void encrypt_input()
{
     int key=0;
     int len=0;
     int loop;
     int loop1;
     int loop2;
     int loop3;
     int loop4;
     int other;
     char input[256];
     int encn[256];
     char encrypted[256];
     
     srand (time(NULL));
     printf("Please Enter The text to be encrypted\nThe first 256 Chars will only be counted\n");
     fflush(stdin);
     fgets(input,256,stdin);
     len=strlen(input);
     //for(
     //input=toupper(input);
     key=rand() %25 +1;
     printf("Your random Encryption number is %d\n",key);
     printf("You will need this to unencrypt you text later on\n\n");
     /// changes nalpha numbers to key
     for(loop1=0;loop1!=25;loop1++)
     {
         nalpha[loop1]=nalpha[loop1]+key;
         if(nalpha[loop1]>25)
         {
            nalpha[loop1]=(nalpha[loop1]-26)-key;
         }
     }
     ////
     
     for(loop2=0;loop2!=len;loop2++)
     {
         if(isalpha(input[loop2]))
         {
             for(loop3=0;loop3!=25;loop3++)
             {
                 if(input[loop2]==alpha[loop3])
                 {
                    encn[loop2]=nalpha[loop3];
                 }
             }
         }
         else
         {
             encrypted[loop2]='#';
         }
     }
     
     for(loop4=0;loop4!=len;loop4++)
     {                              
        other=encn[loop4];
        encrypted[loop4]=alpha[other];
     }
     puts(encrypted);
     system("pause");
}

Hey Berwick,

I looked over your program briefly. Are you running this program on Mac or Windows XP?

@Jmknight...he's obviously using windows...don't know about xp...

@berwick53
Are you sure you are giving your input in UPPERCASE ?

otherwise your 'if' in line 112 will fail and you'll get that "this program stopped working" error (a stack overflow), which i presume is what you'r getting...right?


first solve this and then dig deeper, as there are other gotchas in your code.

Keep exploring...
Happy debugging..:)

This article has been dead for over six months. Start a new discussion instead.