Encrypt Decrypt

Im trying to do this problem but its only reading the encryption portion and not decrpyting after.

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
  #include "stdafx.h"
#include <iostream>
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main()
{
	int x;
	cout << "Enter 4 digit number to be encrypted:" << endl;
	cin >> x;

	int a, b, c, d;

	a = ( x / 1000 ) + 7;
	b = ( x / 100 ) + 7;
	c = ( x / 10 ) + 7;
	d = x + 7;
	
	a = a % 10;
	b = b % 10;
	c = c % 10;
	d = d % 10;

	cout << "Encryption complete!" << endl;
	cout << c << d << a << b << endl;
	
	int y;
	cout << "Enter 4 digit number to be decrypted:" << endl;
	cin >> y;

	int c, d, a, b;

	a = (y / 10);
	b = y;
	c = (y / 1000);
	d = (y / 100);

	if ( a < 7 )
	{
		a = a + 3;
	else
		a = a - 7;
	}

	if (b < 7)
	{
		b = b + 3;
	else
		b = b - 7;
	}

	if (c < 7)
	{
		c = c + 3;
	else
		c = c - 7;
	}

	if (d < 7)
	{
		d = d + 3;
	else
		d = d - 7;
	}


	cout << "Decryption complete!" << endl;
	cout << a << b << c << d << endl;
    
	system("pause");

	return 0;
}


So this is the outcome:
"Enter 4 digit number to be encrypted:
I enter 1234
Enryption complete!
0189
Press any key to continue . . ."

But i want it to then ask me to decrypt. I cant figure that part out! Help please!
No offence, but I have absolutely no idea how the heck your code got past compilation. First of all, int a, b, c, d gets re-declared in the decryption. This would cause a compile-time error. Second of all, an if/else block should look like this:
1
2
3
4
5
6
if(statement) {
    code;
}
else {
    code;
}

not
1
2
3
4
5
if(statement) {
    code;
else
    code;
}

This would sure cause you an error no matter what compiler you are using.
@Tyler T is right.... you have not written the if-else statement correctly

also there is a logical error

here's the correction for the decryption function, replace this in your original 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
a = (y / 10);
	b = y;
	c = (y / 1000);
	d = (y / 100);
	a = a % 10;
	b = b % 10;
	c = c % 10;
	d = d % 10;

if ( a < 7 )
	{
		a = a + 3;
	}
	else
	{
		a = a - 7;
	}

	if (b < 7)
	{
		b = b + 3;
	}
	else
	{
		b = b - 7;
	}

	if (c < 7)
	{
		c = c + 3;
	}
	else
	{
		c = c - 7;
	}

	if (d < 7)
	{
		d = d + 3;
	}
	else
	{
		d = d - 7;
	}



	cout << "Decryption complete!" << endl;
	cout << a << b << c << d << endl;


you didn't add the a=a%10; statement which is why you couldn't decipher the code correctly

PS:

instead of writing this

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
if ( a < 7 )
	{
		a = a + 3;
	else
		a = a - 7;
	}

	if (b < 7)
	{
		b = b + 3;
	else
		b = b - 7;
	}

	if (c < 7)
	{
		c = c + 3;
	else
		c = c - 7;
	}

	if (d < 7)
	{
		d = d + 3;
	else
		d = d - 7;
	}


over and over

you could create a function, it saves time and also is easy to update


HOPE IT HELPS :)
Last edited on
Topic archived. No new replies allowed.