Thank you very much for your help! I finally got my program to work correctly. I was trying to write a program that simulates the German enigma machine used in World War II. You set various setting and type in message and it encodes it for you. Then you reset the machine type in your cyphertext and it gives you the plaintext. I'll post it for giggles:
Code:
#include<stdio.h>/*Needed for input/output*/
#include<string.h>/*needed for strlen()*/
int main(int argc, char *argv[])
{
/*Varialbe Declarations*/
char rotor[5][26]=
{"EKMFLGDQVZNTOWYHXUSPAIBRCJ",/*Rotor I*/
"AJDKSIRUXBLHWTMCQGZNPYFVOE",/*Rotor II*/
"BDFHJLCPRTXVZNYEIWGAKMUSQO",/*Rotor III*/
"ESOVPZJAYQUIRHXLNFTGKDCMWB",/*Rotor IV*/
"VZBRGITYUPSDNHLXAWMJQOFECK"};/*Rotor V*/
char ref[26]="YRUHQSLDPXNGOKMIEBFZCWVJAT";/*reflector*/
char notch[5]="QEVJZ";/*notches in the rotors*/
int order[3];/*rotor order*/
char pos[3];/*starting position of the chosen rotors*/
char rings[3];/*adjustable rings*/
char plaintext[256];/*message to be encrypted*/
int i, j,count=0;/*dummy variables for encoding each character and incrementing rotors*/
int n=0;
int ch,length;
int flag=0;
char junk;/*some stream chracters are causing bugs*/
printf("Which 3 rotors would you like to use in order? (1 through 5)\n");
scanf("%i %i %i",&order[0],&order[1],&order[2]);/*accept user choices of rotors, can use multiple of the same rotor*/
scanf("%c",&junk);
printf("What letters would you like to start on?\n");
scanf("%c %c %c",&pos[0],&pos[1],&pos[2]);/*collect starting positions*/
pos[0]=toupper(pos[0]);
pos[1]=toupper(pos[1]);
pos[2]=toupper(pos[2]);
scanf("%c",&junk);
printf("Where would you like to place the rings?(3 letters)\n");
scanf("%c %c %c",&rings[0],&rings[1],&rings[2]);
rings[0]=toupper(rings[0]);
rings[1]=toupper(rings[1]);
rings[2]=toupper(rings[2]);
scanf("%c",&junk);/*collect some garbage*/
printf("What is your message?\n");
fgets(plaintext,255,stdin);
printf("Your message is encoded as:\n");
length=strlen(plaintext);
for(;count<length;count++)/*keep reading in letters until the end of the string*/
{
ch=plaintext[count];
ch=toupper(ch);
if (!isalpha(ch))
continue;
/* Step up first rotor */
pos[0]++;
if (pos[0]>'Z')
pos[0] -= 26;
/* Check if second rotor reached notch last time */
if (flag)
{
/* Step up both second and third rotors */
pos[1]++;
if (pos[1]>'Z')
pos[1] -= 26;
pos[2]++;
if (pos[2]>'Z')
pos[2] -= 26;
flag=0;
}
/* Step up second rotor if first rotor reached notch */
if (pos[0]==notch[order[0]-1])
{
pos[1]++;
if (pos[1]>'Z')
pos[1] -= 26;
/* Set flag if second rotor reached notch */
if (pos[1]==notch[order[1]-1])
flag=1;
}
/* Rotors (forward) */
for (i=0; i<3; i++)
{
ch += pos[i]-'A';
if (ch>'Z')
ch -= 26;
ch -= rings[i]-'A';
if (ch<'A')
ch += 26;
ch=rotor[order[i]-1][ch-'A'];
ch += rings[i]-'A';
if (ch>'Z')
ch -= 26;
ch -= pos[i]-'A';
if (ch<'A')
ch += 26;
}
/* Reflecting rotor */
ch=ref[ch-'A'];
/* Rotors (reverse) */
for (i=3; i; i--)
{
ch += pos[i-1]-'A';
if (ch>'Z')
ch -= 26;
ch -= rings[i-1]-'A';
if (ch<'A')
ch += 26;
for (j=0; j<26; j++)
if (rotor[order[i-1]-1][j]==ch)
break;
ch=j+'A';
ch += rings[i-1]-'A';
if (ch>'Z')
ch -= 26;
ch -= pos[i-1]-'A';
if (ch<'A')
ch += 26;
}
n++;
putchar(ch);
if (n%5==0)
{
if (n%55==0)
putchar('\n');
else
putchar(' ');
}
}
return 0;
}