Thread: Problem with decryption (. turn to A's)

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Problem with decryption (. turn to A's)

    I've created this program to decrypt sentences with a key that shifts the alphabet. The problem with it is that when i have any non-Alphabetic characters, the punctuation or numbers will turns it into a Alphabetic character for some reason. I cannot find the problem with the code. I know the error occurs when it's checking for the uppercase letters because uppercase letters are always displayed. If you guys could shed any light onto this that would be great. I also commented out the quick fix I made...but it does not allow some characters as well.

    Code:
    #include <iostream>
    #include <fstream> //read from file
    using namespace std;
    
    int main()
    {
    	int offset;
    	const int ENCRYPTED = 250;
    	char message[ENCRYPTED];
    	char AlphabetLower[]="abcdefghijklmnopqrstuvwxyz";
    	char AlphabetHigher[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char AlphabetLowerKey[26];
    	char AlphabetHigherKey[26];
    //	char OtherChars[]="1234567890- ;:,.?!$&*()=+-";
    	int length;
    	int k = 0;
    	int l = 0;
    	int m = 0;
    	int n = 0;
    	int loop = 1;
    	int num = 0;
    	int arraynum = 0;
    
    	cout << "Please enter the encrypted message: ";
    	cin.getline (message, ENCRYPTED, '\n');
    	cin.ignore(100, '\n');
    	cout << "Please enter the decryption key: ";
    	cin >> offset;
    
    	int a = offset; //Have to be initialized after offset is inputted
    	int b = offset;
    
    	length = strlen(message);
    
    	for (a; a < 26; a++)
    	{
    		AlphabetLowerKey[k]=AlphabetLower[a];
    		k++;
    		
    	}
    	for (b; b < 26; b++)
    	{
    		AlphabetHigherKey[l]=AlphabetHigher[b];
    		l++;
    	}
    
    	for (m; m < offset; m++)
    	{
    		AlphabetLowerKey[k]=AlphabetLower[m];
    		k++;
    	}
    	for (n; n < offset; n++)
    	{
    		AlphabetHigherKey[l]=AlphabetHigher[n];
    		l++;
    	}
    	while (arraynum < length)
    	{
    		while (loop == 1)
    		{			
    			if (message[arraynum] == AlphabetLower[num])
    			{
    				message[arraynum] = AlphabetLowerKey[num];
    				loop = 0;
    			}
    			else
    				if (message[arraynum] == AlphabetHigher[num])
    				{
    					message[arraynum] = AlphabetHigherKey[num];
    					loop = 0;
    				}
    				else
    //					if (message[arraynum] == OtherChars[num])
    //					{
    //						loop = 0;
    //					}
    //					else
    						num++;
    		}
    		num = 0;
    		loop = 1;
    		arraynum++;
    	}
    	cout << message << endl;
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You can check whether or not a single character is alphabetical, numerical, punctuation, etc. Have a look at the <cctype> library - in particular, isalpha()

    Also, i'd reccomend researching C++ strings from the <string> header, instead of C-style null-terminated character arrays... you should find they make your life alot easier. (C++ strings automatically determine their own size, and have a wide variety of functions attached which make general manipulation much simpler).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM