string into double

I've been working on some code to convert a string into a double but I am not allowed to use c_string. I have no idea where to start.I have tried stod like many sites have said but the program wont run. any tips to were to go.

closed account (48T7M4Gy)
http://www.cplusplus.com/reference/string/stod/?kw=stod
Ive tried following that but I keep getting an error. Did I write it wrong or missing something.

1
2
p = number.length();
	change = stod(number, p);


it keeps saying stod doesnt match argument list.
Last edited on
You can also use std::stringstream

1
2
3
4
5
6
7
    std::string strDouble = "54.298"; // declare and initialize string with double part
    std::stringstream ss(strDouble); // construct stringstream object with string

    double myDouble;
    ss >> myDouble; // extract from stream and put into double variable

    std::cout << myDouble; // print double 

@arslan7041 i tried inputting the string stream but it keeps giving my an error at ss.
1
2
3
      stringstream ss(number); 
	ss >> change;
	cout << change;
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string strDouble = "54.298";
  std::string::size_type sz;     // alias of size_t

  double earth = std::stod (strDouble,&sz);
  std::cout << earth << '\n';
  return 0;
}


Did I write it wrong or missing something.
Yep, it works OK. And assuming the other one here does, you've got two to pick from! :)
For my method, you need to #include<sstream>
I just saw that. I added the include and it worked fine. Thanks a lot.
Good to hear :)
Topic archived. No new replies allowed.