数据验证 [英] data validation

查看:78
本文介绍了数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何验证数据?如果IE具有类型为int的变量X,并且i

将使用cin>> X;我如何确保他们不回答

信?还是一个符号?

How do i validate data? IE if a have a variable X of type int, and i
promt a user with cin >> X; how do i make sure they don''t answer with a
letter? or a symbol?

推荐答案

< gu ****** @ gmail.com>写道...
<gu******@gmail.com> wrote...
我如何验证数据?如果IE具有类型为int的变量X,并且我将使用cin>>提示用户: X;我如何确保他们不回信?或符号?
How do i validate data? IE if a have a variable X of type int, and i
promt a user with cin >> X; how do i make sure they don''t answer with a
letter? or a symbol?




这在常见问题解答中解释( http://www.parashift.com/c++-faq-lite/


gurumare写道:
gurumare wrote:
我如何验证数据?如果IE具有类型为int的变量X,并且我将使用cin>>提示用户: X;我如何确保他们不回信?或符号?
How do i validate data? IE if a have a variable X of type int, and i
promt a user with cin >> X; how do i make sure they don''t answer with a
letter? or a symbol?




使用std :: getline(cin,myString)来拉取字符串。这让用户点击

< Enter>转动输入。


然后致电:


char * end = NULL;

strtol( myString.c_str(),10,& end);


测试''结束''以确定它是否真正指向myString.end()。这给了一个快速,简单,稳健的验证。


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces




Use std::getline(cin, myString) to pull a string. That lets the user hit
<Enter> to turn-around the input.

Then call:

char * end = NULL;
strtol(myString.c_str(), 10, &end);

Test ''end'' to see if it truly points to myString.end(). That gives a quick,
simple, and robust validation.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces



" Phlip" < pH值******* @ yahoo.com>写了...
"Phlip" <ph*******@yahoo.com> wrote...
gurumare写道:
gurumare wrote:
我如何验证数据?如果IE具有类型为int的变量X,并且我将使用cin>>提示用户: X;我如何确保他们不回信?或者符号?
How do i validate data? IE if a have a variable X of type int, and i
promt a user with cin >> X; how do i make sure they don''t answer with a
letter? or a symbol?



使用std :: getline(cin,myString)来拉取字符串。这让用户点击
< Enter>转动输入。

然后调用:

char * end = NULL;
strtol(myString.c_str(),10,& end) ;

测试''结束''以确定它是否真正指向myString.end()。这给了一个快速,简单,强大的验证。



Use std::getline(cin, myString) to pull a string. That lets the user hit
<Enter> to turn-around the input.

Then call:

char * end = NULL;
strtol(myString.c_str(), 10, &end);

Test ''end'' to see if it truly points to myString.end(). That gives a
quick,
simple, and robust validation.




对于其中一个,我可能会宣称''结束'为' const char *。并且它是否保证

c_str()返回的指针实际上存活了足够长的时间以便''结束''

在检查其值后的某个时间仍然有效?我想知道

井实现如何处理std :: string和std :: strXXX库的混合

函数特别用于检查''end''是否指向myString .END()" ;.

你确定c_str()返回一个指针,其最后一个元素与

一起返回'std :: string :: end()'返回的是什么?我无法在

标准中找到任何保证。


V



For one, I''d probably declared ''end'' as const char*. And is it guaranteed
that the pointer returned by c_str() actually survives long enough for ''end''
to still be valid some time later when you check its value? I wonder how
well implementations handle this mix of std::string and std::strXXX library
functions especially for checking if ''end'' "points to myString.end()". Are
you sure that c_str() returns a pointer whose last element coincides with
what ''std::string::end()'' returns? I can''t find any guarantee of it in the
Standard.

V


这篇关于数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆