Hi guys. I was bored today and wanted to make a program that did substitution ciphers. I made it and am now testing the actual Algorithm for it but it is not working. It will replace the first letter its told to but if you are substituting 2 letters for 2 other letters or even the whole alphabet for something else for a true substitution cipher it will not work. I know i should be using Strings instead of Char Arrays and that some of the Variable names arent great just remember i was writing this up quick and i am not familiar with strings enough to implement them in this situation. Thanks for any and all help you can give.
Code:#include <iostream> #include <fstream> #include <string> #include <windows.h> using namespace std; int main() { //single-letter substitutions cipher program //infinite loop while(1) { //clear screen system("cls"); //title cout<<"Mono-Alphabetic Substitution Cipher\n"; cout<<"===================================\n\n"; //menu cout<<"(1) Substitute\n"; cout<<"(2) Exit\n\n"; //choice int mchoice; cout<<"Choice: "; cin>>mchoice; cin.ignore(); //evaluate if(mchoice==1) { //encrypt char ptext[50]; char lettersto[30]; char replacements[30]; cout<<"Text to encrypt: "; cin.getline(ptext, 50); cout<<"Letters to replace in text: "; cin.getline(lettersto, 26); cout<<"Replace with what (respectively): "; cin.getline(replacements, 26); //begin replacement int i=0; int j=0; int k; int n; int L=(strlen(ptext)); int M=(strlen(lettersto)); //check some stuff if(M != (strlen(replacements))) { cout<<"Replacement Mismatch\n"; cin.ignore(); break; } //for loops //ENCRYPTION------------------------------ for(i; i<M; i++) { k = int(lettersto[i]); for(j; j<L; j++) { n = int(ptext[j]); if(k == n) { //replace letter ptext[j] = replacements[i]; } } } //ENCRYPTION----------------------------- cout<<ptext; cout<<"\n\n"; cin.ignore(); continue; } if(mchoice==2) { //exit cout<<"Exitting..."; Sleep(1500); break; } else{ cout<<"\nIncorrect Choice!!!\n"; cin.ignore(); } } return 0; }



LinkBack URL
About LinkBacks



