Encrypter?

I am trying to make a program that encrypts a 10 letter message.
I just started but I keep getting errors.
Can you please tell me what my problem is?

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
#include <iostream>
using namespace std;

char a;
char b;
char c;
char d;
char e;
char f;
char g;
char h;
char i;
char j;

int main()
{
    cout <<"The Encryptifier 9000" << endl;;
    cin >> char a;
    cin >> char b;
    cin >> char c;
    cin >> char d;
    cin >> char e;
    cin >> char f;
    cin >> char g;
    cin >> char h;
    cin >> char i;
    cin >> char j;

    if(char a == 'a')
    {
        char a = 'b'
    }
    else if(char a == 'b')
    {
        char a = 'c'
    }
}


Thanks
line 17 - remove extra semicolon at the end
lines 18 - 27, 29, 31, 33, 35 - remove char - you don't need this, you've already declared these variables to be char types

line 31, 35 - add semicolon at end of statements
You look to be missing semi colons after the assignment statements inside the if loops.

Also by putting "char" before your variables inside the if statements you are actually creating new local variables that will be deleted once they go out of scope at the end of your if statement. You only need to denote the type of variable the first time you create said variable.
Last edited on
Thanks.

you dont need to use char in front of the variables except when you declare them at the top.

1
2
cout <<"The Encryptifier 9000" << endl;;
cin >> a;


if(a == 'a')

For information purposes you code would get quite messy if you were using this method for long passwords, I would suggest using a Array to store the password then access it using the index of the array.


To extend on my last post, you can also use strings if you prefer. The below example uses XOR to encrypt the message to show you the basics of using a loop to go through the whole string (or array if you prefer)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;


int main()
{
	string Message;

	cout << "Please Enter Message: ";
	getline(cin, Message);

	for (int i = 0; i < Message.length(); i++)
		Message[i] = Message[i] ^ 43; // xor 43

	cout << "Encrypted:  " << Message << endl;
	
	return 0;

}
So it was working for 1 character but when I added another it would not encrypt my message.
Here is the code
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
using namespace std;

char a;
char b;
char c;
char d;
char e;
char f;
char g;
char h;
char i;
char j;

int main()
{
    cout <<"The Encryptifier 9000" << endl;
    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;
    cin >> e;
    cin >> f;
    cin >> g;
    cin >> h;
    cin >> i;
    cin >> j;

    if(a == 'a')
    {
        a = 'b';
    }
    else if(a == 'b')
    {
        a = 'c';
    }
    else if(a == 'c')
    {
        a == 'd';
    }
    else if(a == 'd')
    {
        a == 'e';
    }
    else if(a == 'e')
    {
        a == 'f';
    }
    else if(a == 'f')
    {
        a == 'g';
    }
    else if(a == 'g')
    {
        a == 'h';
    }
    else if(a == 'h')
    {
        a == 'i';
    }
    else if(a == 'i')
    {
        a == 'j';
    }
    else if(a == 'j')
    {
        a == 'k';
    }
    else if(a == 'k')
    {
        a == 'l';
    }
    else if(a == 'l')
    {
        a == 'm';
    }
    else if(a == 'm')
    {
        a == 'n';
    }
    else if(a == 'n')
    {
        a == 'o';
    }
    else if(a == 'o')
    {
        a == 'p';
    }
    else if(a == 'p')
    {
        a == 'q';
    }
    else if(a == 'q')
    {
        a == 'r';
    }
    else if(a == 'r')
    {
        a == 's';
    }
    else if(a == 's')
    {
        a == 't';
    }
    else if(a == 't')
    {
        a == 'u';
    }
    else if(a == 'u')
    {
        a == 'v';
    }
    else if(a == 'v')
    {
        a == 'w';
    }
    else if(a == 'w')
    {
        a == 'x';
    }
    else if(a == 'y')
    {
        a == 'z';
    }
    else if(a == 'z')
    {
        a == 'a';
    }
    else
    {
        cout <<"Invalid character" << endl;
        return 0;
    }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    if(b == 'a')
    {
        b = 'b';
    }
    else if(b == 'b')
    {
        b = 'c';
    }
    else if(b == 'c')
    {
        b == 'd';
    }
    else if(b == 'd')
    {
        b == 'e';
    }
    else if(b == 'e')
    {
        b == 'f';
    }
    else if(b == 'f')
    {
        b == 'g';
    }
    else if(b == 'g')
    {
        b == 'h';
    }
    else if(b == 'h')
    {
        b == 'i';
    }
    else if(b == 'i')
    {
        b == 'j';
    }
    else if(b == 'j')
    {
        b == 'k';
    }
    else if(b == 'k')
    {
        b == 'l';
    }
    else if(b == 'l')
    {
        b == 'm';
    }
    else if(b == 'm')
    {
        b == 'n';
    }
    else if(b == 'n')
    {
        b == 'o';
    }
    else if(b == 'o')
    {
        b == 'p';
    }
    else if(b == 'p')
    {
        b == 'q';
    }
    else if(b == 'q')
    {
        b == 'r';
    }
    else if(b == 'r')
    {
        b == 's';
    }
    else if(b == 's')
    {
        b == 't';
    }
    else if(b == 't')
    {
        b == 'u';
    }
    else if(b == 'u')
    {
        b == 'v';
    }
    else if(b == 'v')
    {
        b == 'w';
    }
    else if(b == 'w')
    {
        b == 'x';
    }
    else if(b == 'y')
    {
        b == 'z';
    }
    else if(b == 'z')
    {
        b == 'a';
    }
    else
    {
        cout <<"Invalid character" << endl;
        return 0;
    }
    cout <<"\n\n" << a << b << endl;
}


Your source code doesn't have enough IF statements in it ;-)

This could be shortened right down a huge amount, basically looking at your code you are moving up 1 letter for each letter typed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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

int main()
{

	string message;

	cout << "Enter Message: ";
	getline(cin, message);

	for (int i = 0; i < message.length(); i++)
		message[i] = message[i] + 1;
	
	cout << message;
	return 0;

}


I would suggest looking up For loops: http://www.cplusplus.com/doc/tutorial/control/

Last edited on
Thanks

Your welcome, if you are not sure what my example is doing please ask. There is no point in me just handing you code over and you not understanding it.

I agree with Softrix's suggestion to handle this another way instead of a whole lot of if/else statements.

That being said - you have this in quite a few places, this is just one example
1
2
3
4
  else if(a == 'c')
    {
        a == 'd'; // <-- you want an assignment statement here, not ==
    }



Edit: If you're not allowed to use strings, you can also do this with a character array.

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
#include <iostream>

int main() 
{
	const int size = 10;
	char passArray[size]={};
	
	std::cout << "Enter 10 digit password " << std::endl;
	for (int i = 0; i < size; i++)
		std::cin >> passArray[i];
	
	std::cout << "You entered ";
	for (int i = 0; i < size; i++)
		std::cout << passArray[i];
	
	//increment each character by 1
	for (int i = 0; i < size; i++)
		passArray[i]++;
	
	std::cout << "\nEncrypted: ";
	for (int i = 0; i < size; i++)
		std::cout << passArray[i];
	
	return 0;
Enter 10 digit password 
You entered abcdefghij
Encrypted: bcdefghijk
Last edited on
Topic archived. No new replies allowed.