Help converting string to double

I'm trying to get 3 lines of input (numbers) from a file "in2.txt" and do some calculations on them. I'm having trouble converting the strings to doubles so that I can calculate them with the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> 

using namespace std;

int main () 
{
  string x1, y1, z1; 
  double x, y, z, smallest, largest, sum, mean, median;  
  fstream in2;
  in2.open("in2.txt");
  	    
  if (in2)
  {
  std::getline( in2, x1 );
  std::getline( in2, y1 );
  std::getline( in2, z1 );
  }

//Convert string to double
  x = strtod (x1, NULL);
  y = strtod (y1, NULL);
  z = strtod (z1, NULL);  
  


The error I get is

26 C:\Dev-Cpp\fileio2.cpp cannot convert `std::string' to `const char*' for argument `1' to `double strtod(const char*, char**)'


Last edited on
It appears as though strtod doesn't accept a parameter of std::string.

Change lines 23, 24 and 25 pass the c-string equivalent of the strings (there's a function for that):
http://www.cplusplus.com/reference/string/string/c_str/

Another approach would be to use the new std::stod function (if your compiler supports it):
http://www.cplusplus.com/reference/string/stod/
Topic archived. No new replies allowed.