Encrypting string

I was having trouble encrypting a string of 36 characters using the Caesar Cypher. The Caesar cypher using a shift parameter to shift each alphabet a couple of characters so encrypt the code.

So a string of abcde, with a shift parameter of 3 would become, defgh.

Here's is a sample code of my encryption function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(int i=0; i<36; i++)
	{
		shift=plain[i]+parameter;
		
		if(plain[i]>=65 && plain[i]<=90)
		{
			if(shift>=91)
			{
				shift-=26;
			}
		}
		else if(plain[i]>=97 && plain[i]<=122)
		{
			if(shift>=123)
			{
				shift-=26;
			}
		}
				
		cout << shift;
	}


Now the problem is that, for small characters a-z (ASCII 97-122), a shift parameter above 5 starts printing random values, I think. Help?
Don't see anything. What are the 'random' values you get?
By the way, it would be a lot clearer if instead of plain[i]>=65 && plain[i]<=90 you wrote plain[i]>='A' && plain[i]<='Z'.
The values I'm getting are actually greater than 128 on the ASCII chart. Like for example, for a shift of 26 of "abcdefghijklmnopqrstuvwxyz" i should be getting the same thing over again. But i get "abcde" alright. The rest of characters are different. This does not happen when I'm dealing with capital alphabets which are from 65 to 90
I see now. 'z' = 122, if parameter = 6, shift = 128, which is in fact a negative number (google two's complement to see why). To solve this, make shift either unsigned of larger than a char.
Awesome, life saver man. Thanks.
Topic archived. No new replies allowed.