Hey DaniWeb, I'm having a tough time with this piece of code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
            if(e[i] == "{")
            {
                    e[i] = "0";
            }
            if(e[i] == ":")
            {
                    e[i] = "a"
       }
       return e;
}

It should "encrypt" the string, using only alpha-numeric characters, as in z+1 = {, so z is converted to 0, and 9+1 = :, so 9 is converted to a. The error I get is 49 C:\Dev-Cpp\files\ver1\main.cpp: ISO C++ forbids comparison between pointer and integer Can anybody help me with this?

Thanks!

Hey buddy !!!
the problem with ur code is that while comparing in the if condition,
if(e=="{")...
you are using double " " marks.
here, if e is a string, then any index of that string will be a character.
for example if e="hello", then e[4]='o'.

so while comparing you should use only a single ' mark in the if condition because what you are actually doing is comparing a character only.

so your code should be like this:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i )
       {
            ++e[i];
            if(e[i] == '{')
            {
                    e[i] = '0';
            }
            if(e[i] == ':')
            {
                    e[i] = 'a';
            }
       }
       return e;
}

hope this is clear :)

Hey DaniWeb, I'm having a tough time with this piece of code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
            if(e[i] == "{")
            {
                    e[i] = "0";
            }
            if(e[i] == ":")
            {
                    e[i] = "a"
       }
       return e;
}

It should "encrypt" the string, using only alpha-numeric characters, as in z+1 = {, so z is converted to 0, and 9+1 = :, so 9 is converted to a. The error I get is 49 C:\Dev-Cpp\files\ver1\main.cpp: ISO C++ forbids comparison between pointer and integer Can anybody help me with this?

Thanks!

Great, the code worked fine! although, I cannot convert a 9 into an 'a'. That is, when I enter 9, I get 0. How can I fix this?

Well dude..if u run the above code i have pasted..it definitely converts 9 into 'a';
if not give me a test case where this does not happen..i shall take a look and get bak to u..

Great, the code worked fine! although, I cannot convert a 9 into an 'a'. That is, when I enter 9, I get 0. How can I fix this?

Here is where it does not work for me:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
            switch(e[i]) {
                 case 0:
                      e[i] = 'a';
                 case '{': 
                      e[i] = '0';
                 case ':': 
                      e[i] = 'a';
                 case '[': 
                      e[i] = '0';
                 default: e[i] = e[i];
            }
       }
       return e;
}

If you need, I can post the entire code for you, it is for a KeygenMe program.

Oh yea, Ive tried copying your code directly, it works now.

I guess maybe I replaced a charachtar or something? Heh..

Thanks anyway!

Dude there seems to be a fundamental flaw in ur code.
the switch-case statements that u have written here are quite wrong.
In the switch statements, we often use a break; statement, so that the flow of the program for a particular case stops when we find the desired value.

Here, in ur program, what is actually happening is that the cases continue to be checked even after finding the desired value.
That is the reason u are unable to get 'a' when u key 9 into the program.

So here's the correct code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
 

            switch(e[i]) {
                 case 0:
                      e[i] = 'a';
                      break;
                      
                 case '{': 
                      e[i] = '0';
                      break;
                      
                 case ':':
                      e[i] = 'a';
                      break;
                      
                 case '[': 
                      e[i] = '0';
                      break;
                      
                 default: e[i] = e[i];
            }

       }
       return e;
}

Ur code should run fine nw... :)

Oh yea, Ive tried copying your code directly, it works now.

I guess maybe I replaced a charachtar or something? Heh..

Thanks anyway!

Dude there seems to be a fundamental flaw in ur code.
the switch-case statements that u have written here are quite wrong.
In the switch statements, we often use a break; statement, so that the flow of the program for a particular case stops when we find the desired value.

Here, in ur program, what is actually happening is that the cases continue to be checked even after finding the desired value.
That is the reason u are unable to get 'a' when u key 9 into the program.

So here's the correct code:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i ){
            ++e[i];
 

            switch(e[i]) {
                 case 0:
                      e[i] = 'a';
                      break;
                      
                 case '{': 
                      e[i] = '0';
                      break;
                      
                 case ':':
                      e[i] = 'a';
                      break;
                      
                 case '[': 
                      e[i] = '0';
                      break;
                      
                 default: e[i] = e[i];
            }

       }
       return e;
}

Ur code should run fine nw... :)

i want to ask a question that confuse me. what is " ++e;" for

thats just program specific....
dont bother about that... that was not the problem here...
its just a part of program !! :)

i want to ask a question that confuse me. what is " ++e;" for

Hey buddy !!!
the problem with ur code is that while comparing in the if condition,
if(e=="{")...
you are using double " " marks.
here, if e is a string, then any index of that string will be a character.
for example if e="hello", then e[4]='o'.

so while comparing you should use only a single ' mark in the if condition because what you are actually doing is comparing a character only.

so your code should be like this:

string encrypt(string e)
{
       for( int i=0; e[i] != '\0'; ++i )
       {
            ++e[i];
            if(e[i] == '{')
            {
                    e[i] = '0';
            }
            if(e[i] == ':')
            {
                    e[i] = 'a';
            }
       }
       return e;
}

hope this is clear :)

i want to ask a question that confuse me. what is " ++e;" for

That is too add one to the character, so if the char was a, it would now be b.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.