Computer Science Canada

char to string

Author:  unoho [ Wed Mar 30, 2011 4:29 pm ]
Post subject:  char to string

hey guys im trying to make a program that will encrypt text. my first step is to alter the text.
for example, if i have "hello" as key, im trying to make it "ifmmp"... like every character should add 1. but it's not working for some reason. any idea wht might be wrong with it?
code:

int encrypt(string key){

    for (int i =0;i<key.length();i++){
        char temp = key.at(i)+1;
        string me = temp;
        key.replace(i,1,me);
    }
    cout <<key << endl;

   //more stuff goes here
}

Author:  Tony [ Wed Mar 30, 2011 4:56 pm ]
Post subject:  RE:char to string

Moved to help (not a tutorial). Also, you've been here for long enough to know that "it's not working for some reason" is not a valid question. In which way does it "not work"?

Author:  unoho [ Wed Mar 30, 2011 6:00 pm ]
Post subject:  RE:char to string

ooppsss...sorry for posting it in the wrong section and not stating the actual problem :S

but ya...i got it to work..all i had to do is
string me;
me = temp;

but thanks tony..i'll remember to ask proper question next time. Smile

Author:  yoursecretninja [ Wed Mar 30, 2011 6:04 pm ]
Post subject:  Re: char to string

code:

int encrypt(string key){

    for (int i =0;i<key.length();i++){
        char temp = key.at(i)+1;
        string me = temp;
        key.replace(i,1,me);
    }
    cout <<key << endl;

   //more stuff goes here
}


I'm guess you're problem is trying to assign the char temp to the string me. You can't convert a character to a string like that. They are different data types. Try using sprintf to create a formatted string and to copy that formatted string into 'me'.

code:
sprintf(me, "%c", temp);


EDIT:
My bad. My knowledge of C is limited to working with primitive types. When I read string me = temp my brain said char me[80] = temp. Anyway, glad you figured it out.

Author:  Velocity [ Wed Nov 23, 2011 9:50 am ]
Post subject:  RE:char to string

Are you trying to created an MD5? or caeser?
chrstring works for me

Author:  md [ Wed Nov 23, 2011 10:24 am ]
Post subject:  RE:char to string

Once again, do not revive dead threads. This your *third* warning.


: