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