ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thank you, I actually found a list of convert functions for C++, it sais atof converts to a float, that for a double you would do something like this:
d = strtod("1234.567");
however, it does not say anything for a string stored ina variable, would I need to do your:
const char *c_ptr=str.c_str();
and then:
d = strtod(c_ptr); ?
or would this work as is:
d = strtod(str);
and last question for you:
what does "cout << c_ptr;" do? does it simply print to see if anything was left over? or does it print what was placed into "d"? or does it do somethign else entirely? (I am a c++ student, halfway through first of the 3 classes my cc offers, I am also taking java, C#, and bash scripting simeltaniously)
Thank you Very much, anyone else out there have the answer for C#?
Originally posted by exodist Thank you, I actually found a list of convert functions for C++, it sais atof converts to a float, that for a double you would do something like this:
d = strtod("1234.567");
however, it does not say anything for a string stored ina variable, would I need to do your:
const char *c_ptr=str.c_str();
and then:
d = strtod(c_ptr); ?
or would this work as is:
d = strtod(str);
and last question for you:
what does "cout << c_ptr;" do? does it simply print to see if anything was left over? or does it print what was placed into "d"? or does it do somethign else entirely? (I am a c++ student, halfway through first of the 3 classes my cc offers, I am also taking java, C#, and bash scripting simeltaniously)
Thank you Very much, anyone else out there have the answer for C#?
atof() returns a double, atoi() returns an int, and atol() returns a long. and yes, if you are
using C++'s string class you will need to convert it to a C-style string for either method to work.
for your last question, (and make sure you notice my edit) cout <<d; prints variable d (to show that d was actually given the numeric value of str).
would I need to add a new using statement to the top of the file?
at the moment I have:
using System;
using System.Collections;
is anything else needed for the conversion?
Nope, the Convert class is in the System namespace, so nothing else should be needed.
BTW, this is in the .Net library from MS. If you are using something like DotGNU, it's possible that it is one of the things that hasn't been implemented yet. I know the last time I took a look at DotGNU it still needed work done on a number of the classes, anyway... An alternative method is the Double.Parse method, which essentially does the same thing as Convert.ToDouble with the exception that Convert.ToDouble takes more data types than just strings...
Thank you, I am aware of the limitations, I also have visual studio .net on a windows box I just braught up, I get it free through msdn because I am a student, I use it to test.
#include <sstream>
template<typename T>
void string_to_type( const std::string& s, T& t )
{
std::istringstream iss( s );
iss >> t; // check this if error handling is required
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.