Help With Caesar Cipher!

Hi, I have been working on this program all day. It encrypts strings of 3 letters but thats it. And when I reach x it starts to give symbols instead of starting over with the alphabet.. here is my code



using namespace std;

int main()
{
char plainText[100];
char cipherText[100];
cin.get(plainText, 100);

for (int i=0; plainText[i]; i++)
{
if ((int)plainText[i] != 32)
cipherText[i] = plainText[i] + 3;

if(plainText[i] >= 97 && plainText[i] <= 122)
{
if(plainText[i] > 122)
cipherText[i] = plainText[i] - 26;
}


else if (plainText[i] > 90)
cipherText[i] = plainText[i] - 26;


}

cout << cipherText;
cin.get();
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}


i thought that the formula would help, but it does not. What am i doing wrong?
ok, I lost my long message, timeout problem... anyway, I was saying;
1. initialize the char arrays. otherwise, it will have garbage data.
2. quit in for loop when you get null, no need to go up to 100.
3. include iostream
4. dont cast char to int, it's done implicitly for char

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Caesar Chipher
// plain text alphabet will be shifted n unit


#include<iostream>
using namespace std;

int main(){
	char plainText[100]={'\0'}; //if you don't initialize
	char cipherText[100]={'\0'}; // char arrays, you get 
	cin.get(plainText, 100);	 // garbage data
	cout << endl;

	int shift;
	cout << "enter shift unit: ";
	cin >> shift;

	//first convert all letters in plain text
	//to lower case
	for(int i=0; i<100; i++){
		if(plainText[i] < 91 && plainText[i] > 64) //it's upper case
			plainText[i] = plainText[i] + 32; //now it's lower case
		//else; do nothing. it's already lower case
	}

	
	//now we're going to shift
	for(int i=0; i<100; i++){

		if((plainText[i] + shift) > 122) //if shift results out of range
			cipherText[i] = (plainText[i] + shift) - 26;
		else if(plainText[i] == 32) //don't touch spaces
			cipherText[i] = plainText[i];
		else if(plainText[i] == '\0')
			break;	 //end of text, quit.
		else		//plain text char is in range, just shift.
			cipherText[i] = plainText[i] + shift;		 
	}

	cout << cipherText;
	cout << endl;

	return 0;
}

/*
ThiS is A TesT. abc deF ghi JKL zzz XXX yyy WWW

enter shift unit: 7
aopz pz h alza5 hij klm nop qrs ggg eee fff ddd

*/


by the way, I think you need to handle numbers, question/excl. marks etc. too.
Last edited on
I too had made a one program that did the same.
Here's a few tips:
1. Have a look at the ASCII list in Wikipedia : http://en.wikipedia.org/wiki/ASCII
2.Refer to it to keep track of the Numerical values of the Characters.
3. Use strings rather than Arrays.
i.You should use the for loop, with the condition: i < string.size();
ii.The above condition will loop until i equals the size of the string
iii.You need to use [] to access individual characters. This is same as in Arrays
iv.Don't create two strings, rather use only one, just overwrite the previous
v.Use if else statements to check if the character goes beyond the numerical alphabet limit and perform the calculations to change it back to the first version.
4.As the abve poster said, include iostream
thanks for the help!
Topic archived. No new replies allowed.