I am interested in discussing methods for using stringstream
to parse a line with multiple types. I would begin by looking at the following line:
I have built up a more fine tuned version for this, that is able to skip invalid input character wise (without need to separate double
numbers with whitespace characters):
#include
#include
#include
using namespace std;
int main() {
istringstream iss("2.832 1.3067 nana1.678 xxx.05 meh.ugh");
double num = 0;
while(iss >> num || !iss.eof()) {
if(iss.fail()) {
iss.clear();
while(iss) {
char dummy = iss.peek();
if(std::isdigit(dummy) || dummy == '.') {
// Stop consuming invalid double characters
break;
}
else {
iss >> dummy; // Consume invalid double characters
}
}
continue;
}
cout << num << endl;
}
return 0;
}
Output
2.832
1.3067
1.678
0.05
Live Demo