I've made a function that receives a line and a delimiter, separates the line and returns a vector of floats (like the split
function in java).
This is the function:
vector<float> extractNumbers(string line, char delimiter) {
vector<float> a;
float f;
string forNow = "";
for (unsigned int i = 0; i < line.size(); i++) {
if (line.at(i) == delimeter) {
f = ::atof(forNow.c_str());
cout << ::atof(forNow.c_str()) << endl;
a.push_back(f);
forNow = "";
} else {
forNow += line.at(i);
}
}
f = ::atof(forNow.c_str());
cout << f << endl;
a.push_back(f);
return a;
}
This is the text file I'm trying it with:
3 3
1 1 1
1 2 1
1 1 1
I call this function: vector<float> floatLine = extractNumbers(line, ' ');
When I try to print forNow
parameter I receive the numbers just like in the text, but when I print f
or ::atof(forNow.c_str())
I receive a 0 instead of the first 3 in the first line.
Any thoughts?