Substitution Cipher Program Problem...

This is a discussion on Substitution Cipher Program Problem... within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I was bored today and wanted to make a program that did substitution ciphers. I made it and ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Substitution Cipher Program Problem...

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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your for loops don't initialize the counter variable to 0. It looks like you try to do this elsewhere, but it causes a problem in the nested loop because the second time through j is not reset to 0.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Once again, many thanks for all of your help Daved, i really appreciate it!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'll need <cstring> for strlen(), and <sstdlib> for system.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Writing a function called say substitute() would help simplify main()

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The initialization part of a for loop can be empty (actually, all parts can):
    Code:
    for(i; i<M; i++)
    ->
    Code:
    for( ; i<M; i++)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    You might need to read/write your files in binary.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Oh, and strlen() returns type size_t, not int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks guys for all of the suggestions, the program is running fine. Like i said earlier though it was just for fun Thanks again everyone!

    Perhaps someone could help me in my attempt to learn strings and show me how to make this program using strings? Thanks!

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Maybe you should post your C-string code first.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Perhaps someone could help me in my attempt to learn strings and show me how to make this program using strings?

    Convert
    Code:
    char ptext[50];
    to
    Code:
    string ptext;
    Do the same with the other string variables.

    Convert
    Code:
    cin.getline(ptext, 50);
    to
    Code:
    getline(cin, ptext);
    Do the same for the other calls to getline.

    Convert
    Code:
    strlen(ptext)
    to
    Code:
    ptext.length()
    or
    Code:
    ptext.size()
    Do the same for all calls to strlen.

    Your original program should have had #include <cstring> because you used strlen. The new code with C++ strings won't use strlen, so you won't need <cstring>. Your original code didn't use the C++ string class, so it didn't need #include <string>. The new code will use the C++ string class, so you should keep that in there.

    You might be able to take advantage of built-in replace functionality to make your encryption code simpler, but it should still work the way it is with the C++ string class.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Oh yes, and you don't need your continue statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Oh yes, and you don't need your continue statement.

    This is incorrect. If you take it out the program won't work properly.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, you're right. You'd also need to make the if(mchoice==2) into an else if. Never mind then.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 09:45 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21