Simple C++ Encryption/Decryption Program

Ok I think I have everything I need to get the output of:


Encrypted string is: uijt!jt!b!tfdsfu"

Decrypted string is: this is a secret!

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
#include <iostream >
using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
int main( )
{
// create a string to encrypt
char string[ ] = "this is a secret!";
cout << "Original string is: " << string << endl;
encrypt( string );
// call to the function encrypt( )
cout << "Encrypted string is: " << string << endl;
decrypt( string );
// call to the function decrypt( )
cout << "Decrypted string is: " << string << endl;
return 0;
}// main

//encrypt data
void encrypt (char e[] ) 
{
for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) {
for( ; * ePtr != '\0'; ==* ePtr ) --(* ePtr);
}


However I get the following 2 errors when compiling. I have tried moving code around but still to no avail. What am I doing wrong?

D:\Profiles\rdkv34\Desktop\Struct.cpp In function `void decrypt(char*)':

Line 28 D:\Profiles\rdkv34\Desktop\Struct.cpp expected primary-expression before '==' token
The compiler is complaining about the decrypt function;
1
2
3
4
void decrypt( char * ePtr ) {
for( ; * ePtr != '\0'; ==* ePtr ) --(* ePtr);
//                     ^^^^^^  error
}

do you want
1
2
3
void decrypt( char * ePtr ) {
for( ; * ePtr != '\0'; ++* ePtr ) --(* ePtr);
}

Last edited on
closed account (z05DSL3A)
I think line 28 should be:
for( ; * ePtr != '\0'; ++* ePtr ) --(* ePtr);

Edit: beaten to it.

Edit 2: Thanks Duoas (see later)
The above code sould be:

for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
Last edited on
I just changed line 28 and that got the desired output. I knew I was overlooking something small. Thanks for your help
Hi, I'm new to this. I copy and pasted the code and then changed line 28. It compiles and runs but freezes when it gets to the decrypt function. I tried it in Windows and Linux. Compiled with g++ in both cases.

I'm confused by the "* ePtr" part. Should it be initialized to something in the beginning of the for loop? Also it looks like an iterator but up to this point all I've done with iterators requires declareing the iterator in int main() or globally like for (vector<string>::iterator).

Anyone know why it's freezing up on me? Thanks
That's because Grey Wolf made a typo when writing his response. Line 28 should read

for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);

The encode function just adds one to every letter.
The decode function just subtracts one from every letter.

They are essentially the same function, except that the OP wrote one using an integer index into the string, and the other by manipulating the string pointer itself.

Enjoy.
Last edited on
OK thanks, that fixed it. I guess I need to learn about pointers now.

I'll see if I can use a similar algorythm and write it another way with what I do know.
Topic archived. No new replies allowed.