When you're parsing things like this in C++, it's often quite a bit easier if you start with a little stream extractor that just matches literal characters, about like non-conversion characters in a scanf
format string do. For example:
std::istream &operator>>(std::istream &is, char const *sep) {
while (*sep && *sep == is.peek()) {
is.ignore(1);
++sep;
}
if (*sep)
is.setstate(std::ios::failbit);
return is;
}
Using this is pretty straightforward, something along this line:
int main() {
std::stringstream infile(std::string("0-670-82162-4"));
int a, b, c, d;
// read four numbers, separated by dashes:
infile >> a >> "-" >> b >> "-" >> c >> "-" >> d;
// print out the numbers we read:
std::cout << a << '\n' << b << '\n' << c << '\n' << d << '\n';
}
That won't necessarily read an ISBN correctly, as the check digit isn't necessary a normal digit (one non-digit is possible, typically encoded as an 'X').
To deal with that you might want to (for one possibility) read into strings instead. With the code above, you'd just change int a, b, c, d;
to std::string a, b, c, d;
, and it'll be able to read the check digit correctly.
An ISBN is supposed to always be 4 groups of digits, separated by dashes, but the length of each group isn't specified, so trying to read a specific number of other characters between dashes is likely to lead to problems.