I don't know what is wrong with my code. It doesn't run as expected. I just want to encrypt the characters in my cstring using the conditions in the if statements, and output all the results each time the key is iterated (key from 1 to 100), but even the iteration doesn't work properly. It starts from 9. I get the following warnings during compilation, too:
[Warning] multi-character character constant [-Wmultichar]
[Warning] overflow in implicit constant conversion [-Woverflow]
Example of output (as you see it starts iteration from 9 not 1 and then goes up to 100):
Encrypted using key (9) : n
Encrypted using key (10) : o
Encrypted using key (11) : p
Encrypted using key (12) : q
Encrypted using key (13) : r
Encrypted using key (14) : s
Encrypted using key (15) : t
Encrypted using key (16) : u
Encrypted using key (17) : v
Encrypted using key (18) : w
Encrypted using key (19) : x
Encrypted using key (20) : y
Encrypted using key (21) : z
Encrypted using key (22) : {
Encrypted using key (23) : |
Encrypted using key (24) : }
Encrypted using key (25) : ~
Encrypted using key (26) :
Encrypted using key (27) : !
Then Code:
#include <iostream>
#include <cstring>
using namespace std;
string myfunction(char a[]);
int main()
{
//char a[] = ":mmZ\dxZmx]Zpgy";
//char a[] = {':','m','m','Z',92,'d','x','Z','m','x',']','Z','p','g','y','\0'};
//char a[] = {58,109,109,90,92,100,120,90,109,120,93,90,112,103,121,0};
char a[] = {'e','o','i','/0'};
cout << myfunction(a) << endl;
return 1;
system("pause");
}
string myfunction(char a[])
{
string encrypted;
for (int i=0; i<strlen(a)-1; i++){
cout << " Taking first character for encryption : " << a[i] << endl;
for (int key =1; key<=100; key++)
{
if (a[i]+key > 126){
encrypted= (a[i]+key)-95;
cout << "Encrypted using key (" <<key<<") : " << encrypted << endl;}
else{
encrypted= a[i]+key;
cout << "Encrypted using key (" <<key<<") : " << encrypted << endl;}
}
}
return ("\nGood Job!");
}