How can I convert string numbers to double via atof immediately into an array?

Code itself

Typical error

  • You yourself answered how. What's the question? Maybe you do not know about .c_str() ? - AlexGlebe 7:08 pm
  • Error code and text should be inserted as text. - VTT

1 answer 1

Once you have indicated that the code is in C ++, then use C ++ tools. The string from STL has quite convenient functions for converting to numbers: stoi(), stod() , etc. The only restriction is that you need to use C ++ 11 (or newer). Here is an example of use:

 #include <iostream> // std::cout #include <string> // std::string, std::stod int main () { std::string orbits ("365.24 29.53"); std::string::size_type sz; // alias of size_t double earth = std::stod (orbits,&sz); double moon = std::stod (orbits.substr(sz)); std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n"; return 0; }