5

I'm having trouble converting a string into a double. My string has been declared using the "string" function, so my string is:

string marks = "";

Now to convert it to a double I found somewhere on the internet to use word.c_str(), and so I did. I called it and used it like this:

doubleMARK = strtod( marks.c_str() );

This is similar to the example I found on the web:

n1=strtod( t1.c_str() );

Apparently, that's how it's done. But of course, it doesn't work. I need another parameter. A pointer I believe? But I'm lost at this point as to what I'm suppose to do. Does it need a place to store the value or something? or what?

I also need to convert this string into a integer which I have not begun researching as to how to do, but once I find out and if I have errors, I will edit this out and post them here.

2
  • 1
    Have you looked at the documentation for strtod()? Jan 23, 2012 at 3:32
  • No I have not, but I'll get on that right away.
    – Robolisk
    Jan 23, 2012 at 3:42

3 Answers 3

11

Was there a reason you're not using std::stod and std::stoi? They are at least 9 levels more powerful than flimsy strtod.

Example

#include <iostream>
#include <string>

int main() {
  using namespace std;
  string s = "-1";
  double d = stod(s);
  int i = stoi(s);
  cout << s << " " << d << " " << i << endl;
}

Output

-1 -1 -1

If you must use strtod, then just pass NULL as the second parameter. According to cplusplus.com:

If [the second parameter] is not a null pointer, the function also sets the value pointed by endptr to point to the first character after the number.

And it's not required to be non-NULL.

10
  • lmao. Uhm, the reason behind why I haven't used those is because I've never learned them, or heard of them, and they never came up on my google adventure. Care to explain how they work? or should I go back to my great voyage across google? Greatly appreciated.
    – Robolisk
    Jan 23, 2012 at 3:39
  • @Robsta yes, I only learned about them here on SO. You can click on the name of the function in the answer to go to the doc page for each of them. If you can't decipher the documentation then let me know and I'll give an example Jan 23, 2012 at 3:40
  • @J.F.Sebastian thanks very much, I never mind people editing my answers to improve them. Jan 23, 2012 at 3:42
  • I happen to like strtod () :)
    – paulsm4
    Jan 23, 2012 at 3:43
  • From my understanding, all you need is the string you'd like to convert, and a pointer? Then you get the return value you so desired? How'd I do?
    – Robolisk
    Jan 23, 2012 at 3:46
2

Back in the Bad Old Dark Days of C, I'd do something ugly and unsafe like this:

char sfloat[] = "1.0";
float x;
sscanf (sfloat, "%lf", &x);

In C++, you might instead do something like this:

// REFERENCE: http://www.codeguru.com/forum/showthread.php?t=231054
include <string>
#include <sstream>
#include <iostream>

template <class T>
bool from_string(T& t, 
                 const std::string& s, 
                 std::ios_base& (*f)(std::ios_base&))
{
  std::istringstream iss(s);
  return !(iss >> f >> t).fail();
}

int main()
{
  int i;
  float f;

  // the third parameter of from_string() should be 
  // one of std::hex, std::dec or std::oct
  if(from_string<int>(i, std::string("ff"), std::hex))
  {
    std::cout << i << std::endl;
  }
  else
  {
    std::cout << "from_string failed" << std::endl;
  }

  if(from_string<float>(f, std::string("123.456"), std::dec))
  {
    std::cout << f << std::endl;
  }
  else
  {
    std::cout << "from_string failed" << std::endl;
  }
  return 0;
} 

Personally, though, I'd recommend this:

http://bytes.com/topic/c/answers/137731-convert-string-float

There are two ways. C gives you strtod which converts between a char array and double:

// C-ish: 
input2 = strtod(input.c_str(), NULL);

The C++ streams provide nice conversions to and from a variety of types. The way to use strings with streams is to use a stringstream:

// C++ streams: 
double input2;
istringstream in(input); 
input >> input2;
1
  • 1
    This. This also help a crap load. Man, thank you for you time and input, I appreciate it (:
    – Robolisk
    Jan 23, 2012 at 3:50
1

We can define a stringTo() function,

#include <string>
#include <sstream>

template <typename T>
T stringTo(const std::string& s) {
  T x;
  std::istringstream in(s);
  in >> x;
  return x;
}

Then, use it like

std::cout << stringTo<double>("-3.1e3") << " " << stringTo<int>("4");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.