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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
unholysavagery
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 73
Joined: Tue Oct 28, 2008 4:27 am

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

Post 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?
This is the internet, men are men, women are men and children are the FBI.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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()
Last edited by programmerinprogress on Wed Nov 26, 2008 3:49 pm, edited 1 time in total.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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?
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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 :(
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
unholysavagery
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 73
Joined: Tue Oct 28, 2008 4:27 am

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

Post by unholysavagery »

I found out I was doing it right from the start but had a problem with something else. :oops:
This is the internet, men are men, women are men and children are the FBI.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post by avansc »

jesus, what happened do atoi, itoa, atof ... etc... see this is why you young people need to learn some C manners.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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:
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
mllarson
Chaos Rift Regular
Chaos Rift Regular
Posts: 183
Joined: Fri Nov 14, 2008 5:20 pm
Location: Minnesota, where "Cold as Hell" has real meaning...

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

Post 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"?
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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 ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

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

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post 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...
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

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

Post by cypher1554R »

avansc wrote:wtf...
:lol: read my sig..
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

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

Post by avansc »

cypher1554R wrote:
avansc wrote:wtf...
:lol: read my sig..
lolz. i was about to ask your for some of your hallucinogens.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

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

Post 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:
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post by dandymcgee »

Take the red pill, and shut up.. I shoved the blue up my ass, cry over it if you want.
:lol: 8-)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply