-3

Newbie question but I have a string that gets 3 numbers, such as:

144.3 432.3 532.3

Now I define 3 floats with

float x;
float y;
float z;

How can I put all the values inside of them? Where,

x = 144.3; 
y = 432.3; 
z = 532.3;
ssell
  • 6,429
  • 2
  • 34
  • 49
  • Possible duplicate of [How do you convert a string into an array of floats?](http://stackoverflow.com/questions/9986091/how-do-you-convert-a-string-into-an-array-of-floats) – Cecilia Mar 23 '17 at 00:19

2 Answers2

2

You can use std::stringstream:

std::stringstream ss("144.3 432.3 532.3");
float x, y, z;
ss >> x >> y >> z;
gsemac
  • 852
  • 7
  • 17
1

Try the stof standard library function.

std::string orbits ("686.97 365.24");
std::string::size_type sz;     // alias of size_t

float mars = std::stof (orbits,&sz);
float earth = std::stof (orbits.substr(sz));
ssell
  • 6,429
  • 2
  • 34
  • 49
samus
  • 6,102
  • 6
  • 31
  • 69