Caesar cipher
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Caesar cipher

  1. #1
    Join Date
    Oct 2008
    Posts
    4

    Question Caesar cipher

    Hi everyone
    I have been trying to create a simple encryption program it will read the txt from a .txt and then if plaintext encrypt it or if encrypted decrypt it to plaintext using the caesar's cipher I just can't get my head around as I am only new to c++ programming

    Any help or ideas would be great

    Here's what I have so far:

    Code:
    #include <iostream>//for input and output
    #include <fstream>// for working with files
    
    #define isupper(a) ((a)>= 'A' && (a)<= 'Z') 
    #define islower(a) ((a)>= 'a' && (a)<= 'z') 
    
    using namespace std;
    ifstream inFile;
    ofstream outFile;
    char PlainText[100], CipherText[100] ;
    int main() 
    {
        {
           outFile.open("test.txt");
           (!outFile.eof() << PlainText[100]);
           for ( int i = 0 ; PlainText[i] ; i++ )
           {
               if ( isupper(PlainText[i]) )
                CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
               else if ( islower(PlainText[i]) )
                CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
               else CipherText[i] = PlainText[i] ;
            }
           outFile << CipherText << endl;
        }
    inFile.close();
    system("pause");
    return 0;
    }
    Thanks in advance
    Last edited by hitman_38; October 27th, 2008 at 10:40 PM.

  2. #2
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: Caesar cipher

    Don't know much about Ceasar cipher, but please read this before posting.
    Also you can edit your post according to that.
    http://www.codeguru.com/forum/announ...nouncementid=6
    You just divided by zero, didn't you?

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Caesar cipher

    You should use code tags to make it easier to follow your code. See Odiee's link for info on how to do that.

    From a casual glance at your code, it appears that there are several problems. inFile is never opened nor read from. PlainText and CipherText are never initialized. This line:

    Code:
    (!outFile.eof() << PlainText[100]);
    appears to be missing something; perhaps a while or if. It may be that it is misplaced and should be a few lines up (as indicated by the superfluous opening '{' ).

    The loop for your actual encryption/decryption looks sound.

  4. #4
    Join Date
    Oct 2008
    Posts
    4

    Re: Caesar cipher

    Ok I see the mistakes that are there but I not sure how to fix them. I'm still learn. I added some comments if that will help.
    I know the encryption/decryption part works fine with cin & cout's, but I'm just confused about reading & writing to a text file instead of the cin & cout's?

    Code:
    #include <iostream>//for input and output
    #include <fstream>// for working with files
    
    #define isupper(a) ((a)>= 'A' && (a)<= 'Z') 
    #define islower(a) ((a)>= 'a' && (a)<= 'z') 
    
    using namespace std;
    
    ifstream inFile;//file handle for data writing
    ofstream outFile;//file handle for data reading
    char PlainText[100], CipherText[100] ;
    
    int main() 
    {
       cout << "A program that encrypt/decrypt's text files" << endl << endl;
       cout << "Now encrypting/decrypting ..." << endl << endl;
        {
           //open file
           outFile.open("test.txt");
           if (!outFile.eof() << PlainText[100]);
           for ( int i = 0 ; PlainText[i] ; i++ )
           {
               if ( isupper(PlainText[i]) )//run encrypt/decrypt
                CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
               else if ( islower(PlainText[i]) )
                CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
               else CipherText[i] = PlainText[i] ;
            }
           //write to file
           outFile << CipherText << endl;
        }
    //close file
    outFile.close();
    system("pause");
    return 0;
    }

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Caesar cipher

    You're still not using inFile at all. Is it supposed to be the input text? Immediately after you open outFile, your sending your PlainText buffer to it thereby overwriting anything in the file. Are you supposed to be reading PlainText from outFile? Try:
    Code:
      outFile >> PlainText;
    but I suspect you really want to read it from inFile.

  6. #6
    Join Date
    Oct 2008
    Posts
    4

    Re: Caesar cipher

    I'm not sure where or how to use the infile. It is meant to open the file with outfile read it to the Plaintext buffer then it goes through the encryption/decryption changing to CipherText then write to the file.

    I tried
    Code:
    outFile >> PlainText;
    But it gives me an error
    expected '(' before "outfile"
    am I suppose to use this instead of
    Code:
    (!outFile.eof() << PlainText[100]);
    Last edited by hitman_38; October 29th, 2008 at 10:42 PM.

  7. #7
    Join Date
    Oct 2008
    Posts
    4

    Re: Caesar cipher

    I have uploaded the .cpp file if that will help. I am completely stumped on what to do to make this program work
    Attached Files Attached Files

  8. #8
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Caesar cipher

    Sounds like you need to learn how to do simple file reading & writing--forget about encryption for now. First try to write a simple program that reads text from one file then writes that text to another file. If you have problems with that, post it here. Since it is a simpler problem of basic file read/write, you should be able to post it using code tags as opposed to an attachment. More people will bother to help if they don't have to download something.

  9. #9
    Join Date
    Mar 2009
    Posts
    3

    Arrow Re: Caesar cipher

    I too have the very problemof c++ caesar encryption.
    Can any one pliz just give me a sample working code with illustrations on how they work?

  10. #10
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Caesar cipher

    What are you having trouble with? The cipher itself, or the file I/O associated with plaintext to ciphertext files?

  11. #11
    Join Date
    Jul 2009
    Posts
    6

    Re: Caesar cipher

    maybe this site will help you for file access (read / write )

    http://www.cplusplus.com/doc/tutorial/files/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center