Skip to main content
gamedev.net
🔒 Locked

string conversion

Started by smelvin Jan 7, 2003 at 4:40 PM 4 replies 250+ views
Original Post
smelvin
smelvin
Hopefully a really silly question. How do I convert a string (not a char[]) to a float? I can''t seem to use atof as it says it needs a char[]. Many thanks
Miserable
Miserable
You can get the char* buffer of a string with the std::sting::c_str() method. Alternatively, I believe that you could use a stringstream to input into a float.
smelvin
smelvin
Thanks for the reply, but I am not sure how to use the std::string::c_str().

I have an array of (string) words, and each word is referenced by words[n]. Using atof(words[n]) doesn''t work. How can I use the above to cast the string into a char[].

Sorry, if I appear dum, I am just getting back into c/c++ after a good few years of and its like learning all over again. the old saying is true - "If you don''t use it, you lose it!"

Many thanks again

Scott
civguy
civguy
atof(words[n].c_str())
Thunder_Hawk
Thunder_Hawk

float nums[10];
for (int n = 0; n < 10; n++) {
nums[n] = atof(words[n].c_str());
}


Get the idea?

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
smelvin
smelvin
Cancel last post, reply was to quick.

Got it!

Many thanks.

Scott

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.