Page 1 of 2

I don't know how to use std::string?

Posted: Wed Nov 26, 2008 2:57 pm
by unholysavagery
Well I wanted to just convert a int to a char* but everywhere I look they tell you that you should use a std::string. I found a few samples but none work, could someone show me how to convert a int to a char* the right way and explain whats going on?

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 3:19 pm
by programmerinprogress
EDIT: just re-read your post, and my explanation was rubbish, so heres a better one

use stringstream <sstream> to stream your integer value into the stringstream buffer, into a string like this

Code: Select all


#include<iostream>
#include<sstream>
#include<string> 

using namespace std; 

int main()
{
  string TheString; 
  stringstream TheStream; 
  int TheInt = 5; 

  TheStream << TheInt;  // stream the integers into the stringstream  (changed this to <<)
  TheString = TheStream.str() // convert the stream into  string (I fixed this)


return 0; 
}
EDITED: I didn't go with my gut and use the << operator like I should have, and I used c_str() instead of str()

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 3:31 pm
by Falco Girgis

Code: Select all

TheString = TheStream.c_str() // convert the stream into  string 
Will that work? c_str() returns a const char*, not a string if it's a "string" object. I haven't used stringstream objects. Is it different?

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 3:33 pm
by programmerinprogress
oh no!

one minor correction, its not c_str(), it's just str()

thanks for pointing that one out, I havent used it in a while :lol:

oh and the arrows have to be pointing the way you would do cout (<<) not cin (>>)

my bad :(

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 4:32 pm
by unholysavagery
I found out I was doing it right from the start but had a problem with something else. :oops:

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 4:33 pm
by avansc
jesus, what happened do atoi, itoa, atof ... etc... see this is why you young people need to learn some C manners.

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 4:36 pm
by programmerinprogress
avansc wrote:jesus, what happened do atoi, itoa, atof ... etc... see this is why you young people need to learn some C manners.

they (as in I :lol: ) don't call me programmerinprogress for nothing :mrgreen:

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 4:52 pm
by mllarson
programmerinprogress wrote:
avansc wrote:jesus, what happened do atoi, itoa, atof ... etc... see this is why you young people need to learn some C manners.

they (as in I :lol: ) don't call me programmerinprogress for nothing :mrgreen:
So when you're a master are you going to be known as "programmerprogresscomplete"?

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 4:59 pm
by programmerinprogress
see that's the catch, you can never be the best when it comes to programming, because things are always improving, and even the best programmer makes mistakes from time to time, thus the programmerinprogress alias is here to stay ;)

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 5:19 pm
by cypher1554R
programmerinprogress wrote:see that's the catch, you can never be the best when it comes to programming, because things are always improving, and even the best programmer makes mistakes from time to time, thus the programmerinprogress alias is here to stay ;)
Son, mother earth will take you if you like it or not. 8-)

Program "programmerinprogress" exited with code 0x0000

what then? :)

When the virtual memory of("You") is released, and you return to the source of unlimited intelligence.. Everyone's pretty much complete there. Everyone is ONE 8-) Take the red pill, and shut up.. I shoved the blue up my ass, cry over it if you want.

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 6:09 pm
by avansc
cypher1554R wrote:
programmerinprogress wrote:see that's the catch, you can never be the best when it comes to programming, because things are always improving, and even the best programmer makes mistakes from time to time, thus the programmerinprogress alias is here to stay ;)
Son, mother earth will take you if you like it or not. 8-)

Program "programmerinprogress" exited with code 0x0000

what then? :)

When the virtual memory of("You") is released, and you return to the source of unlimited intelligence.. Everyone's pretty much complete there. Everyone is ONE 8-) Take the red pill, and shut up.. I shoved the blue up my ass, cry over it if you want.

wtf...

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 6:26 pm
by cypher1554R
avansc wrote:wtf...
:lol: read my sig..

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 7:54 pm
by avansc
cypher1554R wrote:
avansc wrote:wtf...
:lol: read my sig..
lolz. i was about to ask your for some of your hallucinogens.

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 8:19 pm
by Arce
avansc wrote:
cypher1554R wrote:
avansc wrote:wtf...
:lol: read my sig..
lolz. i was about to ask your for some of your hallucinogens.
XD, I loled. :lol:

Re: I don't know how to use std::string?

Posted: Wed Nov 26, 2008 8:24 pm
by dandymcgee
Take the red pill, and shut up.. I shoved the blue up my ass, cry over it if you want.
:lol: 8-)