1

I have a series of strings like this "50 50 10"

Each number is supposed to represent the x, y, and z values of an origin. I want to convert each number of this string into an actual float.

I tried using atof, but that gave me back just the first number, 50 in this case.

Any ideas?

Thank you!

user2775084
  • 95
  • 1
  • 4
  • 11
  • possible duplicate of [How to parse space-separated floats in C++ quickly?](http://stackoverflow.com/questions/17465061/how-to-parse-space-separated-floats-in-c-quickly) – sehe Sep 24 '13 at 23:29

2 Answers2

3

Use a istringstream,

int main() {

    string s = "50 50 50";

    istringstream instream;
    instream.str(s);

    double a, b, c;
    instream >> a >> b >> c;
    return 0;
}
CS Pei
  • 10,869
  • 1
  • 27
  • 46
0

The best way to do this would be to use the string stream. This site: has a great tutorial for it. It will also allow for direct casting from string to whatever type you need. (In this case float.)

Dylan Lawrence
  • 1,503
  • 10
  • 32