It is not an easy task for input streams in C ++ to do the same. The scanf function gets the entire expected format: "%d %*s %d" and can look ahead to determine what is happening.
On the other hand, the >> operator is simply trying to satisfy the current input parameter.
You have a chance to write your own istream manipulator so that there are inputs before reaching a digit.
Try this my naive code:
template<typename C, typename T> basic_istream<C, T>& eat_until_digit(basic_istream<C, T>& in) { const ctype<C>& ct = use_facet <ctype<C>> (in.getloc()); basic_streambuf<C, T>* sb = in.rdbuf(); int c = sb->sgetc(); while (c != T::eof() && !ct.is(ctype_base::digit, c)) c = sb->snextc(); if (c == T::eof()) in.setstate(ios_base::eofbit); return in; } int main() { int first, second; cin >> first >> eat_until_digit >> second; cout << first << " : " << second << endl; }
You can expand and improve the code above to achieve what you need.
source share