数字输入的输入验证 [英] input validation for numeric input
本文介绍了数字输入的输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对这个C ++世界很陌生,试图为数字密码写一个输入验证函数。这是我到目前为止:
I'm very new to this C++ world and trying write a input validation function for numeric password. This is what I got so far:
#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;
}
它对正确的值有效,但不会破坏循环有效输入。任何想法,我在这里失踪? Cheers !!
It's working just fine for incorrect value(s) but not breaking out of the loop upon valid entry. Any idea what I'm missing here? Cheers!!
来自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()
感谢大家(特别是詹姆斯·坎泽)帮助我。这个东西在这里工作。
New code: using
getline()
and validation as a string
Thanks everyone (especially James Kanze) for helping me out. This thing is pretty much working here.
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
:
This looks like line oriented input. In which case, the usual solution
is to use 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;
}
这篇关于数字输入的输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文