Hi all,
I'm having some trouble with an assignment where I have to encrypt or decrypt a four digit integer. The encryption and decryption isn't really a problem for me, it's the section for user options. Every time I come to the point where the user has the option to exit, I seem to have trouble executing that task. Please see below.
Code:#include <stdio.h> #include <stdlib.h> void encrypt(int number1); void decrypt(int number2); int main(void) { int number; char option, option2; printf("Encode or Decode? (e/d)"); scanf("%c", &option); if(option == 'e'){ printf("Enter four digit number: "); scanf("%d", &number); encrypt(number); } else if(option == 'd'){ printf("Enter four digit number: "); scanf("%d", &number); decrypt(number); } else{ printf("please choose an option."); } printf("\nContinue? (y/n) "); scanf("%c", &option2); if(option2 == 'y'){ do{ printf("Encode or Decode? (e/d)"); scanf("%c", &option); if(option == 'e'){ printf("Enter four digit number: "); scanf("%d", &number); encrypt(number); } else if(option == 'd'){ printf("Enter four digit number: "); scanf("%d", &number); decrypt(number); } else{ printf("please choose an option."); } printf("\nContinue? (y/n) "); scanf("%c", &option2); }while(option2 != 'n'); } else{ printf("Please remember to secure your password before exiting.\n"); } system("pause"); return 0; } void encrypt(int number1) { int i; int j = 0; int temp; int code[4]; int a, b, c, d, m, n, o, p; for(i = 1000; i >= 1; i /= 10){ temp = number1 % i; code[j] = (number1 - temp) / i; number1 = temp; j++; } m = code[0]; n = code[1]; o = code[2]; p = code[3]; m += 7, n += 7, o += 7, p += 7; /*add 7 to digit*/ m %= 10, n %= 10, o %= 10, p %= 10; /*replace digit with remainder of 10*/ a = m * o/m; /*swap first digit with third*/ c = o * m/o; /*swap third digit with first*/ b = n * p/n; /*swap second digit with fourth*/ d = p * n/p; /*swap second digit with fourth*/ printf("Encrypted number is: %d%d%d%d", a, b, c, d); /*print new number*/ } void decrypt(int number2) { int t = 0; int s; int temp; int code[4]; int a, b, c, d, i, j, k, l; for(s = 1000; s >= 1; s /= 10){ temp = number2 % s; code[t] = (number2 - temp) / s; number2 = temp; t++; } i = code[0]; j = code[1]; k = code[2]; l = code[3]; a = i * k/i; /*swap first digit with third*/ c = k * i/k; /*swap third digit with first*/ b = j * l/j; /*swap second digit with fourth*/ d = l * j/l; /*swap second digit with fourth*/ a += 3, b += 3, c += 3, d += 3; /*simplified - remainder of ten, then minus seven*/ printf("Decrypted number is: %d%d%d%d", a, b, c, d); /*print new number*/



LinkBack URL
About LinkBacks



