why does only half my code work?

Hello
I wrote the below program to turn encrypt, it turns "a to z" "b to y" "c to x" etc.
but the problem is, it is only working on some letters about half and it looks like only the last half of the alphabet.
any suggestions?

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

int main ()
{
string method, map, word, str;
int encryption, decryption;


    
   cout<<"What is the method (encryption or decryption)?"<<endl;
   cin>>method;
   
   
        if (method=="encryption")
        {
            cout<<"What is the translation map (type 'default' to use default):"<<endl;
            cin>>map;
                        if (map=="default")
                        {
                            cout<<"What is the single word to translate:"<<endl;
                            cin>>word;
                                    
                                
                                
                                                       
                            
                                        string str=word;
                                             for (unsigned i=0; i<str.length(); i++)
                                               {
                                                  if (str.at(i)=='a')
                                                      str.at(i)='z';
                           
                                                  if (str.at(i)=='b')
                                                      str.at(i)='y';
                           
                                                  if (str.at(i)=='c')
                                                      str.at(i)='x';
                                                      
                                                  if (str.at(i)=='d')
                                                      str.at(i)='w';
                           
                                                  if (str.at(i)=='e')
                                                      str.at(i)='v';
                           
                                                  if (str.at(i)=='f')
                                                      str.at(i)='u';
                                                  
                                                  if (str.at(i)=='g')
                                                      str.at(i)='t';
                           
                                                  if (str.at(i)=='h')
                                                      str.at(i)='s';
                           
                                                  if (str.at(i)=='i')
                                                      str.at(i)='r';
                                                      
                                                  if (str.at(i)=='j')
                                                      str.at(i)='q';
                           
                                                  if (str.at(i)=='k')
                                                      str.at(i)='p';
                           
                                                  if (str.at(i)=='l')
                                                      str.at(i)='o';
                                                      
                                                  if (str.at(i)=='m')
                                                      str.at(i)='n';
                           
                                                  if (str.at(i)=='n')
                                                      str.at(i)='m';
                           
                                                  if (str.at(i)=='o')
                                                      str.at(i)='l';
                                                      
                                                  if (str.at(i)=='p')
                                                      str.at(i)='k';
                           
                                                  if (str.at(i)=='q')
                                                      str.at(i)='j';
                           
                                                  if (str.at(i)=='r')
                                                      str.at(i)='i';
                                                      
                                                  if (str.at(i)=='s')
                                                      str.at(i)='h';
                           
                                                  if (str.at(i)=='t')
                                                      str.at(i)='g';
                           
                                                  if (str.at(i)=='u')
                                                      str.at(i)='f';
                                                      
                                                  if (str.at(i)=='v')
                                                      str.at(i)='e';
                           
                                                  if (str.at(i)=='w')
                                                      str.at(i)='d';
                           
                                                  if (str.at(i)=='x')
                                                      str.at(i)='c';
                                                      
                                                  if (str.at(i)=='y')
                                                      str.at(i)='b';
                           
                                                  if (str.at(i)=='z')
                                                      str.at(i)='a';
                           
                                               }
                                              cout<<str<<endl;
                                                             
                                                        
                        }
        }
        
        else if (method=="decryption")
        {
            cout<<"you entered decryption"<<endl;
        }
        
        else
        {
            cout<<"not valid input"<<endl;
        }
   
   return 0;
}
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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main ()
{
	string method, map, word, str;
	int encryption, decryption;

	cout<<"What is the method (encryption or decryption)?"<<endl;
	cin>>method;
 
    if (method=="encryption")
    {
		cout<<"What is the translation map (type 'default' to use default):"<<endl;
        cin>>map;
        if (map=="default")
        {
			cout<<"What is the single word to translate:"<<endl;
            cin>>word;

			for(int i = 0; i < word.length(); i++)
				word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
			
            cout<<word<<endl;                 
		}
	}
    else if (method=="decryption")
		cout<<"you entered decryption"<<endl;
    else
		cout<<"not valid input"<<endl;

	system("pause");
	return 0;
}
because, suppose input is "a" then first if is true, so its now "z", .... last if is also true, so its again "a"
Okay now I understand, thank you!

you can add "continue;" for each if. but algorithm is better than hard coding.
char ch = 'a';
ch =219 - int(ch);
Topic archived. No new replies allowed.