C++ if语句没有比较字符串c++;
分配是让用户输入一个密码,该密码具有一定的要求,如长度、大小写字母和数字。当我尝试运行在字符串中添加数字量的for循环时,它似乎跳过了if语句C++ if语句没有比较字符串c++;,c++,string,validation,for-loop,if-statement,C++,String,Validation,For Loop,If Statement,分配是让用户输入一个密码,该密码具有一定的要求,如长度、大小写字母和数字。当我尝试运行在字符串中添加数字量的for循环时,它似乎跳过了if语句 #include <iostream> #include <cstring> #include <stdlib.h> using namespace std; int main() { const int LENGTH = 13; char str[LENGTH]; int size = 0,
#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);
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
常数int长度=13;
字符str[LENGTH];
int size=0,k=0,j=0,r=0;
cout在您的代码中,您正在检查0和10之间的ASCII值,但您希望检查“0”和“9”之间的字符。
您可以检查ASCII表中这些字符的值,并使用这些值:
if(str[z] >= 60 && str[z] <= 71)
if(str[z]>=60&&str[z]='0'&&str[z]str[z]>=0&&str[z]str[z]>='0'&&str[z]@Mitchel0022--'10'
是一个有效的字符常量,但在这里没有用处。不要使用ASCII值。请使用'0'
和'9'
。
if(str[z] >='0' && str[z] <='9')