Google Answers Logo
View Question
 
Q: type cast ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: type cast
Category: Computers > Programming
Asked by: eksolutions-ga
List Price: $2.00
Posted: 12 Nov 2002 18:28 PST
Expires: 12 Dec 2002 18:28 PST
Question ID: 106533
I need the answer of this question in C++ code.
Let's say you have:

char* value = "0.010";
double dValue;

How can you convert char* value to double dValue?
Answer  
Subject: Re: type cast
Answered By: skermit-ga on 12 Nov 2002 18:52 PST
Rated:5 out of 5 stars
 
Hello,

You can't just typecast change the string to double. Instead you have
to use a function in the stdlib.h for string conversion to a double.
From my trusty Deitel & Deitel C++ book (the best I think I've ever
used):

"
Function atof converts its argument -- a string that represents a
floating point number -- to a double value.
"

The prototype on page 824 is:

double atof( const char *nPtr ) // Converts the string nPtr to double

So your usage would have to have an #include <stdlib.h> at the top of
your program and then stick this in the logic:

char* value = "0.010";
double dValue;
dValue = atof( value );

You could shorten the 2nd and 3rd lines to:

double dValue = atof( value );

Glad I could help you out, I've had to overcome this hurdle many a
time post-processing text input.


Search Strategy:

finding my old Deitel & Deitel book and dusting it off


Additional Links:

Online atof resource:
http://www.dinkumware.com/htm_cl/stdlib.html#atof


Thank you for the opportunity to answer your question, if you require
more information, please clarify the question, or if you find this
answer satisfactory, please feel free to rate it. Thank you!

skermit-ga
eksolutions-ga rated this answer:5 out of 5 stars
Thanks, you saved my life. I'm not very familiar with C++ and I was
working on an OpenGL program. This simple function solved all my input
problems...

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy