Help with ceasar cypher

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
//
//Position: 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
//English:  a b c d e f g h i j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
//Caesar : d e f g h i j k l m  n  o  p  q  r  s  t  u  v  w  x  y  z  a  b  c

#include<iostream>
#include<string>
using namespace std;

int main(){
	
	char alpha_array_low[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'
		,'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
	char alpha_array_up[26] =  { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'
		,'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 
	char ceasar_array_low[26] = { 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
		'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c' };
	char ceasar_array_up[26] = { 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
		'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C' };

	char User_entry[1][26];
	char converted_array[1][26];

	
	

	cin >> User_entry[1];

	
		
		for (int b = 0; b < 26; b++){

			if (User_entry[1][b] == alpha_array_low[b]){
				converted_array[1][b] = ceasar_array_low[b];
			}
			if (User_entry[1][b] == alpha_array_up[b]){
				converted_array[1][b] = ceasar_array_up[b];
			}
			

		}
	
	

	
	cout << "Your converted word is " << converted_array[1] << endl;

	system("pause");
	return 0;
}



Hello, i have been trying to do this program above but its not working, i dont know if its not comparing the two dimension arrays with the other 1 dimmension ones.
The output that gives me its the same one i entered, i dont know why.
Please help.
For one thing, you do not have any 2D arrays.
1
2
char User_entry[1][26]; // is the same as char User_entry[26]
char converted_array[1][26]; // same here 

Also you do not always use them as 2D arrays.
1
2
3
cin >> User_entry[1];
...
cout << "Your converted word is " << converted_array[1] << endl;
What is up with the [1]'s? By the way arrays start at index 0 not 1. So who knows what half that stuff is actually doing.

As far as the actual cipher yours is a shift of 3. So you can basically do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string input = "";
std::cout << "Please enter a string to encrypt: ";
std::getline(std::cin, input);

for(int i = 0; i < input.size(); ++i) //I would suggest iterators, I did this for simplicity
{
    if(isalpha(input[i])
    {
        input[i] += 3; //shift right 3;
    }
    if(!isalpha(input[i])) //went out of bounds
    {
        input[i] -= 26; //go back to start
    }
}


There are much more elegant solutions but I am not sure what all you have learned so I tried to make it have only the basics.

Topic archived. No new replies allowed.