我是这个C ++世界的新手,正在尝试为数字密码编写输入验证功能。这是我到目前为止所得到的:
#include <iostream> #include <limits> using namespace std; void isNumeric(int &iN) { while (1) { cin >> iN; if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Only 'numeric' value(s) are allowed: "; continue; } // alpha-numeric entry also not allowed cin.ignore(numeric_limits<streamsize>::max(), '\n'); if (cin.gcount() > 1) continue; // check against the -ve value if (iN <= 0 ) continue; break; } } int main() { int x; cout << "Enter your number: "; isNumeric(x); cout << "You've entered: " << x << endl; return 0; }
对于不正确的值,它工作得很好,但在有效输入时不会中断循环。知道我在这里缺少什么吗?干杯!!
James Kanze脚本的ErroR:
test.cpp: In function ‘bool parseNumber(const string&, int&)’: test.cpp:11:20: error: no match for ‘operator>>’ in ‘text >> results’ test.cpp:11:20: note: candidates are: /usr/include/c++/4.6/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&) /usr/include/c++/4.6/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*) /usr/include/c++/4.6/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) /usr/include/c++/4.6/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&) /usr/include/c++/4.6/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&) /usr/include/c++/4.6/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) /usr/include/c++/4.6/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) test.cpp:11:42: error: ‘const string’ has no member named ‘peek’ test.cpp:11:52: error: ‘EOF’ was not declared in this scope
新代码: 使用 getline() 和 验证作为字符串 感谢所有人(尤其是James Kanze)的帮助。这件事在这里很有效。
getline()
void isNumeric( int &iN ) { string sN; while (1) { getline(cin, sN); bool valNum = true; for ( unsigned iDx=0; iDx < sN.length(); iDx++ ) if ( !isdigit(sN[iDx]) ) { valNum = false; break; } if ( !valNum ) { cout << "Wrong entry; Try again: "; continue; } stringstream sStream (sN ); sStream >> iN; if ( iN<=0 ) { cout << "Cannot be 0; Try again: "; continue; } break; } }
那里还有进一步改进的空间吗?干杯!!
这看起来像是面向行的输入。在这种情况下,通常的解决方案是使用getline:
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; }