GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Sep-2003, 10:02
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road

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  
Old 18-Sep-2003, 11:42
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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:
n1=strtod( t1.c_str() );

Hope this helps
GF
  #3  
Old 18-Sep-2003, 11:50
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road

String to Double


That 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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 06: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 11:33
[function] Show limited words of a string / text JdS PHP Code Library 0 05-Jun-2003 09:25
explain pls.. birdbird MySQL / PHP Forum 1 18-Apr-2003 11:34

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:16.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.