微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数字input的inputvalidation

我对这个C ++的世界很陌生,试图为数字密码一个inputvalidation函数。 这是我到目前为止:

#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; }

对于不正确的值,它工作得很好,但是在有效input时不会破坏循环。 任何想法,我在这里失踪? 干杯!!

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,_CharT2*) /usr/include/c++/4.6/bits/istream.tcc:925:5: note: template<class _CharT,class _Traits> std::basic_istream<_CharT,_CharT&) /usr/include/c++/4.6/istream:709:5: note: template<class _Traits> std::basic_istream<char,_Traits>& std::operator>>(std::basic_istream<char,unsigned char&) /usr/include/c++/4.6/istream:714:5: note: template<class _Traits> std::basic_istream<char,signed char&) /usr/include/c++/4.6/istream:756:5: note: template<class _Traits> std::basic_istream<char,unsigned char*) /usr/include/c++/4.6/istream:761:5: note: template<class _Traits> std::basic_istream<char,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()validationstring谢谢大家(特别是James Kanze)的帮助。 这件事情在这里很有用。

如何使Internet Explorer在某个域中自动login

如何用Apache做客户端证书authentication

lsass.execaching了很多内存和cpu

系统调用获取linux中的机器序列号(用java编程)

如何通过Windows身份validation

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; } }

那里有任何改善的地方吗? 干杯!!

Nginx将内部http服务公开为https子域

_AVrfpGetProcessName是做什么的?

C#解锁工作站

如何在Windows中validation用户名密码

Django Windows身份validation

这看起来像面向行的输入。 在这种情况下,通常的解决方案是使用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; }

如果转换失败,那么流本身的计算结果为false,所以你可以这样做:

int get_int() { int i; std::cout << "Please enter a number: " << std::endl; while(!(std::cin >> i)) { std::cin.clear(); //clear flags //discard bad input std::cin.ignore(std::numeric_limits<std::streamsize>::max()); std::cout << "Incorrect,must be numeric: " << std::endl; } return i; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐