![]() |
|
#1
|
||||
|
||||
string to double C++Hello,
Okay, I have another question for yall. I am having a little problem going from string to double. I am trying to use this function strtod My string name it t1 and my double name is n1 this is what I have, but i know it does not work n1=strtod(t1); if I was going to long I could just do n1=atol(d,t1); but I am not. Yes I know it is not good practice to use such common names at t1 and n1, but this is just testing purposes, I will put this into play, later. Okay thanks for the help. ~Ronald |
|
#2
|
|||
|
|||
|
The problem is that strtod() is a function that has been around since the original C. C didn't have strings as we know it (a class which contains the characters), instead strings were character arrays, with the end being denoted by the a NULL terminator (\0).
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:
Hope this helps GF |
|
#3
|
||||
|
||||
String to DoubleThat is extremely helpful
Okay, I have another question for yall. I am having a little problem going back from double to string. I am currently saving to a text file then reading it back, but I don't like this away at all. Could you show me an easier way please. Okay thanks for the help. ~Ronald No replies found. __________________
This message is a natural product made from recycled electrons. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects. |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Including Maps and strings?? | maddie | C++ Forum | 17 | 05-Jul-2004 07:25 |
| C++ Error that makes no sense! | ArcticTiger | C++ Forum | 1 | 10-Nov-2003 08:14 |
| storing a token pointer as a string | CoreLEx | C Programming Language | 1 | 07-Oct-2003 12:33 |
| [function] Show limited words of a string / text | JdS | PHP Code Library | 0 | 05-Jun-2003 10:25 |
| explain pls.. | birdbird | MySQL / PHP Forum | 1 | 18-Apr-2003 12:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The