I am trying to check input validity. I am given a file, where there are two columns, first one of strings, second one of ints. The separator is any number of spaces. The problem is, I cannot figure out how to properly check if the second item really is an int. Consider this code:
string line; // parameter with a string to be parsed
string name;
int num;
istringstream lineStream(line);
lineStream >> name;
lineStream >> num;
by calling lineStream.good()
I can detect inputs such as "abcd g", but when I put something like "abcd 12a" in, it is parsed to "abcd" and 12. Same with "abcd 12.2" etc.
In Java I could use String.split()
to an array and then parse each whole element, not ignoring any characters, but I do not know what is the correct approach here.
Thanks for hints