分配是让用户输入具有某些要求的密码,例如长度,大写/小写字母和数字。当我尝试运行在循环中添加字符串中的数字量时,它似乎跳过if语句。
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
const int LENGTH = 13;
char str[LENGTH];
int size = 0, k = 0, j = 0, r = 0;
cout << "Enter a password." << endl;
cout << "Must meet these requirements:" << endl;
cout << "At least 10 and at most 12 characters." << endl;
cout << "Any characters entered after 12 will be ignored." << endl;
cout << "At least 2 lower case, and 2 upper case letters." << endl;
cout << "At least 3, but no more than 5 numerical digits." << endl;
cout << "No white spaces." << endl;
cin.getline(str, LENGTH);
//Validating numerical values.
for (int z=0; z < 12; z++)
{
if (str[z] >=0 && str[z] <=10)
{
r++;
}
}
if (r<3 || r>5)
{
cout << "Must have at least 3 but no more than 5 numerical digits."<<endl;
exit (EXIT_FAILURE);
}
答案 0 :(得分:1)
在您的代码中,您正在检查0到10之间的ASCII值,但是您要检查“0”和“9”之间的字符。 您可以在ASCII表上检查这些字符的值,并使用以下值:
if(str[z] >= 60 && str[z] <= 71)
或使用字符:
if(str[z] >='0' && str[z] <='9')