Question: 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.

Answer: 
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;

```
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");

```