这看起来像是面向行的输入。在这种情况下,通常的解决方案是使用
getline:
bool parseNumber( std::string const& text, int& results ){ std::istringstream parser( text ); return parser >> results >> std::ws && parser.peek() == EOF;}int getNumber(){ int results; std::string line; while ( ! std::getline( std::cin, line ) || ! parseNumber( line, results ) ) { std::cin.clear(); std::cout << "only 'numeric' value(s) allowed:"; } return results;}


