建设网站的申请信用卡分期,idea 网站开发,网站设置万事达u卡,网站优化关键词排名c验证用户输入合法性的示例代码
本文介绍c验证用户输入合法性#xff0c;用于检测限定用户输入值。包括#xff1a;1、限定用户输入为整数#xff08;正负整数#xff09;#xff1b;2、限定用户输入为正整数#xff1b;3、限定用户输入为正数#xff08;可以含有小数验证用户输入合法性的示例代码
本文介绍c验证用户输入合法性用于检测限定用户输入值。包括1、限定用户输入为整数正负整数2、限定用户输入为正整数3、限定用户输入为正数可以含有小数4、限定用户输入为一个数5、限定只能输入为英语字母。
提示下面程序需要编译器支持c11标准。
【若你使用Dev-C需要设置打开“工具[T]→编译选项[C]”菜单添加 -stdc11然后按“确定”按钮生效。参见下图红圈处 】 1、限定用户输入为整数
可以是正负整数提供有几种方式的源码供选用
源码1
#include iostream
using namespace std;
//判断输入的字符串是否为整数直至合规才返归其值
int judge(int temp)
{ //对输入的合法性进行判断并返回有效的输入//int temp;cin.sync(); //清空输入流缓冲区cintemp;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(); cintemp;}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;cinstr;while(!isNumber3(str)){cout请输入整数不要输入字母endl;cinstr;}cout 合规输入strendl;//exit(EXIT_SUCCESS);return 0;
}运行效果如下 2、限定用户输入为正整数
提供有几种方式的源码供选用
源码1
#include iostream
#includestring
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 请输入正整数:;nCheckNum(m);cout 合规输入nendl;break; //退出循环 }
}运行效果如下 源码2
#includeiostream
#includealgorithm
#includecstring
#includestdlib.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;is.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;cinn0;//判断输入的n0是否符合要求while(check1(n0)){cout请输入正整数不要输入负数或者小数或字母endl;cinn0;}n s2i(n0);//string转为int存储 cout 合规输入nendl;}运行效果如下 源码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;cinstr;while(!isNumber(str)){cout请输入正整数不要输入负数或者小数或字母endl;cinstr;}cout 合规输入strendl;//exit(EXIT_SUCCESS);return 0;
}运行效果如下 3、限定用户输入为正数
可以含有小数
源码1
#includeiostream
#includealgorithm
#includecstring
#includestdlib.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;is.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;cinn0;//判断输入的n0是否符合要求while(check2(n0)){cout请输入正数不要输入负数或字母endl;cinn0;}n s2d(n0);//string转为double存储 cout 合规输入nendl;
}运行效果如下 源码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;cinstr;while(!isNumber3(str)){cout请输入正数不要输入负数或字母endl;cinstr;}cout 合规输入strendl; 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;cinstr;while(!isNumber3(str)){cout请输入数不要输入字母endl;cinstr;}cout 合规输入strendl;//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;cinstr;while(!isNumber3(str)){cout请输英语字母endl;cinstr;}cout 合规输入strendl;//exit(EXIT_SUCCESS);return 0;
}
运行效果如下 OK! 参考 https://www.jiyik.com/tm/xwzj/prolan_3518.html