cipher

This is a discussion on cipher within the C++ Programming forums, part of the General Programming Boards category; i am trying to write a simple caesar cipher. reading in a text file and using an offset I can ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    cipher

    i am trying to write a simple caesar cipher.
    reading in a text file and using an offset I can decode the file, but handling capitol letters are not working.. here is the code for decryption part

    Code:
    while (inputFile2)
    {
          inputFile2.get(ch);
           if (ch == '@' || ch == '%')
            {
    	outfile << ch;
               
            }
            else 
    	if (islower(ch))
            {    
            ch = ch - offset;
    
            if ( ch < 'a' ) // 'A' for capitals.
            {
               ch = ch + 26;
            }
    	outfile << ch;
           
            }
            else if (ch >= 'A' && ch <= 'Z')
            {
                ch = ch - offset;
    	outfile << ch;
            
            }
            else if (ch == '#')
            {
                cout << endl << endl;
            }
            else if (ch == ' ' || ch == '\t' || ch == '\n')
            {
    	outfile << ch;
            
            }
            else if (ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9')
            {
    	outfile << ch;
              
            }
            }
    as you can see below its not handling capitol letters good.
    output:
    Code:
    :he Declaration of Independence of the :hirteen Colonies
    In C5NG8E99 July 4 1776 
    
    :he unanimous Declaration of the thirteen united 9tates of America

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Better indentation might help reading this but it just seems while the code is adjusting range for small letters (under the section with a mysterious or hintful comment) it won't do this for capital letters.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if ( ch < 'a' ) // 'A' for capitals.
            {
               ch = ch + 26;
            }
    same part for Upper case is missing
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    how would i implement uppercase?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    they onlt thing i can seem to do is make it lower and decrypt it.
    Code:
    if ( isupper ( ch ) ) {
    
    ch = tolower ( ch );
             
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Cipher Program
    By PunchOut in forum C Programming
    Replies: 7
    Last Post: 11-22-2008, 01:12 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  4. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 01:05 AM
  5. my first cipher
    By Nor in forum C++ Programming
    Replies: 8
    Last Post: 07-09-2003, 02:09 PM

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