Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 char to string
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
unoho




PostPosted: 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
}
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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"?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
unoho




PostPosted: 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
yoursecretninja




PostPosted: 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.
Velocity




PostPosted: 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
md




PostPosted: Wed Nov 23, 2011 10:24 am   Post subject: RE:char to string

Once again, do not revive dead threads. This your *third* warning.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: