当前位置: 首页 > news >正文

c++验证用户输入合法性的示例代码

c++验证用户输入合法性的示例代码

本文介绍c++验证用户输入合法性,用于检测限定用户输入值。包括:1、限定用户输入为整数(正负整数);2、限定用户输入为正整数;3、限定用户输入为正数(可以含有小数);4、限定用户输入为一个数;5、限定只能输入为英语字母。

提示:下面程序,需要编译器支持c++11标准。

【若你使用Dev-C++,需要设置:打开“工具[T]→编译选项[C]”菜单,添加 -std=c++11,然后按“确定”按钮生效。参见下图红圈处:

1、限定用户输入为整数

可以是正负整数,提供有几种方式的源码,供选用

源码1

#include <iostream>
using namespace std;
//判断输入的字符串是否为整数,直至合规才返归其值
int judge(int temp)
{      //对输入的合法性进行判断并返回有效的输入//int temp;cin.sync();    //清空输入流缓冲区cin>>temp;while(1){if(cin.fail()||cin.bad()||cin.get()!='\n')    //验证输入是否合法,其中cin.fail()和cin.bad()解决输入字符串和溢出的问题cout<<"错误!请重新输入:"<<endl;    //cin.get()检查输入流中是否还有字符(如果有就表示输入的是形如123r或12.3的错误else break;      //输入合法则跳出循环cin.clear();    //清空输入流缓冲区标志位,以免影响下次输入cin.sync();     cin>>temp;}return temp;
}int main()
{cout<<"请输入整数:"<<endl;int a;	cout<<"合规的输入:"<<judge(a)<<endl;return 0;
}

运行效果如下:

源码2

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;//是整数返回1,否则返回0 
bool isNumber3(const string& str)
{return str.find_first_not_of( "-0123456789" ) == string::npos &&str.front() != '.' && str.back() != '.';
}int main(){string str;cout<<"请输入整数:"<<endl;cin>>str;while(!isNumber3(str)){cout<<"请输入整数,不要输入字母:"<<endl;cin>>str;}cout <<"合规输入:"<<str<<endl;//exit(EXIT_SUCCESS);return 0; 
}

运行效果如下:

2、限定用户输入为正整数

提供有几种方式的源码,供选用

源码1

#include <iostream>
#include<string>
using namespace std;
//判断输入的字符串是否为正整数,若是,则合规返归其值 ,否则给出提示继续 
int CheckNum(int& n)
{int i;string x;  //用来接受输入bool flag = false;while (cin >> x) {for (i = 0; i < x.size(); ++i) {//判断是否为中文if (x[i] & 0x80) {cout << "\n输入错误,请重新输入正确的数字: ";break;}//判断是否为字母或者其它字符if (!isdigit(x[i])) {cout << "\n输入错误,请重新输入正确的数字: ";break;}}if (i == x.size()) {break;   //如果字符串中所有字符都合法,则退出while循环}}n = atoi(x.c_str()); //将string字符串转化为整数return n;    
}int main()
{int m,n;while (true) {cout << "请输入正整数:";n=CheckNum(m);cout <<"合规输入:"<<n<<endl;break;  //退出循环 		}
}

运行效果如下:

源码2

#include<iostream>
#include<algorithm>
#include<cstring> 
#include<stdlib.h>  //为了使用 c_str() 函数 
using namespace std;
//将string转为char数组并判断输入是否为正整数
int check1(string s){char a[s.length()];strcpy(a,s.c_str());int i; for(i = 0;i<s.length();i++){//如果不是数字if(!isdigit(a[i])){return -1; }}return 0; 
}//string 转 int
int s2i(string s)
{return atoi( s.c_str() );
}int main()
{string n0;//判断用户输入用int n;//真正存储的变量cout<<"请输入:"<<endl;cin>>n0;//判断输入的n0是否符合要求while(check1(n0)){cout<<"请输入正整数,不要输入负数或者小数或字母:"<<endl;cin>>n0;}n = s2i(n0);//string转为int存储 cout <<"合规输入:"<<n<<endl;}

运行效果如下:

源码3

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;//是正整数返回1,否则返回0 
bool isNumber(const string& str)
{return !str.empty() &&find_if(str.begin(), str.end(),[](unsigned char c) { return !isdigit(c); }) == str.end();
}int main(){string str;cout<<"请输入正数:"<<endl;cin>>str;while(!isNumber(str)){cout<<"请输入正整数,不要输入负数或者小数或字母:"<<endl;cin>>str;}cout <<"合规输入:"<<str<<endl;//exit(EXIT_SUCCESS);return 0; 
}

运行效果如下:

3、限定用户输入为正数

可以含有小数

源码1

#include<iostream>
#include<algorithm>
#include<cstring> 
#include<stdlib.h>  //为了使用 c_str() 函数 
using namespace std;
//将string转为char数组并判断输入是否为正数
int check2(string s){char a[s.length()];strcpy(a,s.c_str());int i; for(i = 0;i<s.length();i++){//如果不是数字且不是小数点 if((!isdigit(a[i])) && (a[i]!='.')){return -1; }}return 0; 
}//string 转 double
double s2d(string s)
{return atof( s.c_str() );
}int main(){string n0;//判断用户输入用double n;//真正存储的变量cout<<"请输入正数:"<<endl;cin>>n0;//判断输入的n0是否符合要求while(check2(n0)){cout<<"请输入正数,不要输入负数或字母:"<<endl;cin>>n0;}n = s2d(n0);//string转为double存储 cout <<"合规输入:"<<n<<endl;
}

运行效果如下:

源码2

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;//是正数返回1,否则返回0 
bool isNumber3(const string& str)
{return str.find_first_not_of( ".0123456789" ) == string::npos &&str.front() != '.' && str.back() != '.';
}int main(){string str;cout<<"请输入正数:"<<endl;cin>>str;while(!isNumber3(str)){cout<<"请输入正数,不要输入负数或字母:"<<endl;cin>>str;}cout <<"合规输入:"<<str<<endl;   return 0; 
}

运行效果如下:

4、限定用户输入为一个数

包括正负整数、正负小数,但不能含有字母

源码如下

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;//是数返回1,否则返回0 
bool isNumber3(const string& str)
{return str.find_first_not_of( "+-.0123456789" ) == string::npos &&str.front() != '.' && str.back() != '.';
}int main(){string str;cout<<"请输入数:"<<endl;cin>>str;while(!isNumber3(str)){cout<<"请输入数,不要输入字母:"<<endl;cin>>str;}cout <<"合规输入:"<<str<<endl;//exit(EXIT_SUCCESS);return 0; 
}

运行效果如下:

5、限定只能输入为英语字母

源码如下

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;//是英语字母返回1,否则返回0 
bool isNumber3(const string& str)
{return str.find_first_not_of( "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" ) == string::npos &&str.front() != '.' && str.back() != '.';
}int main(){string str;cout<<"请输入英语字母:"<<endl;cin>>str;while(!isNumber3(str)){cout<<"请输英语字母:"<<endl;cin>>str;}cout <<"合规输入:"<<str<<endl;//exit(EXIT_SUCCESS);return 0; 
}

运行效果如下:

OK!

参考 
https://www.jiyik.com/tm/xwzj/prolan_3518.html

http://www.lryc.cn/news/62589.html

相关文章:

  • ctfshow web入门phpcve web311-315
  • gpt.4.0-gpt 国内版
  • 放弃手动测试,快来了解JMeter压测神器的安装和使用吧~~
  • SQL函数
  • 苦熬10年,国产操作系统“归零”,新操作系统上新,跟Excel很像
  • 什么是shell脚本和简单shell脚本练习
  • MySQL MyBatis
  • Leetcode力扣秋招刷题路-0802
  • 编程中最难的就是命名?这几招教你快速上手
  • NUXT规范及常见问题
  • 2023年Q1天猫空调品牌销量排行榜
  • 如何在比特币系统内创造人工生命
  • 除了Figma,再给你介绍10款好用的协同设计软件
  • 信息安全复习五:数据加密标准(DES)
  • Java ---包装类
  • Baumer工业相机中偏振相机如何使用Baumer堡盟GAPI SDK来进行偏振数据的计算转换输出(C#)
  • MSVC(Microsoft Visual C++) 中运行库的链接方式MD和MT的区别
  • 设计模式之解释器模式(C++)
  • 基于MATLAB编程的粒子群算法优化BP神经网络风电功率预测,基于PSO-BP的风电功率预测
  • 开心档之C++ 字符串
  • Java Collection源码分析(JDk corretto 11)
  • 13种权重的计算方法
  • Devops和Gitops区别
  • 拿下多家车企定点!4D毫米波雷达「域」系统首发出道
  • 【FATE联邦学习】FATE联邦学习使用GPU、指定cuda下标
  • 英文数字表达
  • 第11届蓝桥杯省赛真题剖析-2020年6月21日Scratch编程初中级组
  • 部署LVS-NAT群集实验
  • 对待工作的九个级别
  • 第四章 存储结构与管理硬盘