C++ Help :: ASCII Symbol Error in Mono-alphabetic Shift

Background Info:
I'm trying to write a few programs for text encryption, nothing fancy just a little poking around into cryptography, so I decided to start simple and go with a Caesar Cypher mono-alphabetic shift. I've been working on this for the past two nights and ended up hitting a few other issues I've since sorted out. However, this one has me stumped. Thank you!

The Program:
My program uses functions to get input from the user, namely the input file as a location on disk (character array), and an integer for the shift.

The Issue:
I broke the lower case alphabet.
For some reason my encryption algorithm will take care of all upper case letters just fine yet will not encrypt the lower case letters at the end of the alphabet starting with the letter 's' which in my algorithm should be output as 'f' with a shift of 13 which is what I'll be using as the default shift for this post, unless otherwise specified.

The Code Algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void encrypt(ifstream &input, ofstream &output, int shift) {
	//Current location. Where the program is inside the input file.  
	char cL;
	//Get cL as the current location in the input file, loop until EOF. Do not ignore whitespace.
	while (input >> noskipws >> cL) {
		//If the current character is within the bounds of the alphabet
		if(cL >= 'a' && cL <= 'z') {
			//Apply the shift and check to see if the new value is outside of the alphabet. Past 'Z' OR 'z'
			//Bring it back within the alphabet by subtracting 26.
			cL += shift;
			if(cL > 'z') {
				cL -= 26;
			}
		} else if(cL >= 'A' && cL <= 'Z') {
			cL += shift;
			if(cL > 'Z') {
				cL -= 26;
			}
		}
		//Print the results of the encryption to the output file.
		output << cL;
		//int cLN = cL;
		//output << cLN << endl;
	}
}


The Results:
Using an input text file containing both the lower and upper case alphabet in the form:
abcdef...z
ABCDEF...Z

I get the following output for a shift of 13:
nopqrstuvwxyzabcde€‚ƒ„…†‡
 NOPQRSTUVWXYZABCDEFGHIJKLM



Additional Results:

I decided to try an see what was happening by also outputting the integer value next to my characters for a shift of 13. For simplicity's sake I am only providing the output of two working values before the issue:
d100
e101
€-128
-127
‚-126
ƒ-125
„-124
…-123
†-122
‡-121



Reproductions:
The symbols also occur for all shifts greater than 5



Thank you for taking the time to look at my issue. Your help is greatly appreciated.
Last edited on
I solved my issue.

What happened was I applied the shift before I subtracted the 26 in order to put the numbers back within the range of the alphabet. Since ASCII only goes to the 127th value and then the compiler reset it to -128, I was pushing my cL char variable too far past the bounds.

I fixed this by instead creating a predicted value and comparing it to 128, anything >= 128 would have 26 subtracted from it, then the shift is applied.

I simply had my logic backwards.

Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void encrypt(ifstream &input, ofstream &output, int shift) {
	//Current location. Where the program is inside the input file.  
	char cL;
	//Get cL as the current location in the input file, loop until EOF. Do not ignore whitespace.
	while (input >> noskipws >> cL) {
		//If the current character is within the bounds of the alphabet

		if(cL >= 'a' && cL <= 'z') {
			//Check to see if the new value will be outside of ASCII (past 128). Past 'Z' OR 'z'
			//Shift the alphabet. 
			if( (cL + shift) >= 128) {
				cL -=26;
			}
			cL += shift;
			if(cL > 'z') {
				cL -= 26;
			}
		} else if(cL >= 'A' && cL <= 'Z') {
			cL += shift;
			if(cL > 'Z') {
				cL -= 26;
			}
		}
		//Print the results of the encryption to the output file.
		output << cL;
		//int cLN = cL;
		//output << cLN << endl;
	}
}
Topic archived. No new replies allowed.