用户输入类型验证 [英] User input type validation

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

问题描述

大家好,
我需要一个C ++代码片段来验证用户输入的int类型的值.我知道如果用户输入字符,但我不希望输入为字符或任何其他类型,则Visual Studio编译器将对ASCII使用ASCII等效字符.代码的算法类似于:
如果(输入==整数)
接受;
其他
拒绝;
我目前遇到的问题是不能对值和类型进行相等性测试.

Hi all,
I need a C++ code snippet to validate values entered by user to be of type int. I know that the visual studio compiler will use the ASCII equivalent for a char if the user enters characters but I do not want the input to be characters or any other type. The algorithm for the code is something like:
if (input == integer)
accept;
else
reject;
The problem I have presently is that a value and a type cannot be tested for equality.

推荐答案

用户输入的所有内容都是字符串形式,由您的应用程序决定它代表什么类型并进行转换.使用整数解析函数之一,例如 strtol [ ^ ]等,并拒绝任何输入的数字无效.
Everything that is entered by the user is in the form of character strings, it is up to your application to decide what type it represents and to convert it. Use one of the integer parsing functions, such as strtol[^] etc, and reject any input that is not a valid number.


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

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