1

Can anyone help me with a way of converting String to double in vc++?

I can't use atoi, since it converts char to double. But I am using istringstream.

std::istringstream stm; 

double d;
String name = "32.67";

stm.str(name);
stm >>d; 

It will give a compilation error:

error C2664: 'void std::basic_istringstream::str(const std::basic_string &)' :
cannot convert parameter 1 from 'System::String ^' to 'const std::basic_string &'

Please help with different solution or correct this.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
devan
  • 1,643
  • 8
  • 37
  • 62

3 Answers3

4

std::stringstream str() accepts a std::string as an argument. You're passing it a System::String, wherever that comes from. Given the funky ^ symbol you must be using C++/CLI, using .NET strings.

Use std::string unless you are for some reason required to use the .NET library, in which case you need to either use .NET conversion functions, or convert to a std::string (or char* c-string and use the << operator).

Justin Aquadro
  • 2,280
  • 3
  • 21
  • 31
1

As the other responder has suggestion, you are probably using C++/CLI. In that case:

 String ^ name = "32.67";
 double d;
 d = Double::Parse(name);

Note that if the string cannot be parsed into a double, an exception will be thrown. Use Double::TryParse if you want to avoid that (it returns false if the string cannot be parsed).

jonsca
  • 10,218
  • 26
  • 54
  • 62
1

I think it is very simple in vc++/CLR programming.

String ^name = "32.56";
String ^no = "56";

Double number_double = Convert::ToDouble(name); // convert String to double
Int number_int = Convert::ToInt32(no); // convert String to integer
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
devan
  • 1,643
  • 8
  • 37
  • 62