проверка ввода для числового ввода
Я очень новичок в этом мире C ++ и пытаюсь написать функцию проверки ввода для числового пароля. Это то, что я получил так далеко:
<code>#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; } </code>
Он просто отлично работает для неправильных значений, но не прерывает цикл при правильной записи. Есть идеи, что мне здесь не хватает? Ура !!
ErroR from James Kanze's script:<code>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 </code>Новый код:using
getline()
and validation as a string
Thanks everyone (especially James Kanze) for helping me out. This thing is pretty much working here.
<code>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; } } </code>
Есть ли здесь место для дальнейшего улучшения? Ура !!