I don't know how to use std::string?
Moderator: Coders of Rage
- unholysavagery
- Chaos Rift Cool Newbie
- Posts: 73
- Joined: Tue Oct 28, 2008 4:27 am
I don't know how to use std::string?
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.
- programmerinprogress
- 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?
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
EDITED: I didn't go with my gut and use the << operator like I should have, and I used c_str() instead of str()
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;
}
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
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
- Falco Girgis
- 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?
Code: Select all
TheString = TheStream.c_str() // convert the stream into string
- programmerinprogress
- 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?
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
oh and the arrows have to be pointing the way you would do cout (<<) not cin (>>)
my bad
one minor correction, its not c_str(), it's just str()
thanks for pointing that one out, I havent used it in a while
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
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
- unholysavagery
- Chaos Rift Cool Newbie
- Posts: 73
- Joined: Tue Oct 28, 2008 4:27 am
Re: I don't know how to use std::string?
I found out I was doing it right from the start but had a problem with something else.
This is the internet, men are men, women are men and children are the FBI.
Re: I don't know how to use std::string?
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"
Dad, "Yea well I have a fan belt in street fighting"
- programmerinprogress
- 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?
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 ) don't call me programmerinprogress for nothing
---------------------------------------------------------------------------------------
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
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
- mllarson
- 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?
So when you're a master are you going to be known as "programmerprogresscomplete"?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 ) don't call me programmerinprogress for nothing
- programmerinprogress
- 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?
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
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
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Re: I don't know how to use std::string?
Son, mother earth will take you if you like it or not.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
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 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?
cypher1554R wrote:Son, mother earth will take you if you like it or not.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
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 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"
Dad, "Yea well I have a fan belt in street fighting"
- cypher1554R
- Chaos Rift Demigod
- Posts: 1124
- Joined: Sun Jun 22, 2008 5:06 pm
Re: I don't know how to use std::string?
read my sig..avansc wrote:wtf...
Re: I don't know how to use std::string?
lolz. i was about to ask your for some of your hallucinogens.cypher1554R wrote:read my sig..avansc wrote:wtf...
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: I don't know how to use std::string?
XD, I loled.avansc wrote:lolz. i was about to ask your for some of your hallucinogens.cypher1554R wrote:read my sig..avansc wrote:wtf...
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
- dandymcgee
- 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?
Take the red pill, and shut up.. I shoved the blue up my ass, cry over it if you want.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!