Although I quickly determined that one solution was strtod function I had problems getting it to work until I read GID Forum at http://www.gidforums.com/t-1131.html.
I have cut and pasted relevent section here.
Invalid Email Address | Join Date: May 2002 Location: Ireland Posts: 638 |
So, in plain C, to get a string, you'd have
char[] word = {'h', 'e', 'l', 'l', 'o', '\0'};
These were pretty dodgy, since the only way to know the end of a string was to see the NULL character at the end. If the \0 is overwritten (and it can happen pretty easily), then the memory is continually read until a NULL character is found, getting a load of rubbish along the way. And writing to this would overwrite memory needed elsewhere, crashing all.
C++ improved this drastically, with the string class, which stores the length of the string along with a character array - so it doesn't rely on a NULL character.
However (getting back to the point), strtod() function only can accept C style strings, i.e. character arrays. You can convert a C++ string to a C string using a method of the string class, c_str().
word.c_str() - returns a character array with a NULL at the end.
So to answer your question, the syntax to get that working is:
CPP / C++ / C Code:
n1=strtod( t1.c_str() );
No comments:
Post a Comment