所以我要创建一个程序来验证密码是否正确创建。要通过验证,用户必须输入6个或更多字符,至少包含一个小写字母,一个大写字母和一个数字。问题是我必须只使用cstring(当然还有cctype)库而不是字符串库。如果用户创建了错误的密码,程序运行正常,如果他们成功创建了一个好的密码,那么它有时(不确定为什么有时会)崩溃。对此的回答将帮助我巩固我对指针的理解更多的内存分配是动态的。这就是有问题的程序。
#include <iostream>
#include <cctype>
#include <cstring>
bool isVerifyAccepted(char*, int);
using namespace std;
int main() {
const int SIZE = 6;
char *userPass = new char[SIZE];
cout << "Verify you have a good password\na good password has to be at least six characters long\n"
<< "have at least on uppercase and one lowercase letter and at least one digit" <<endl <<endl;
cout << "enter a password below" <<endl <<endl;
cin >> userPass;
int userSizePass = strlen(userPass);
char testPassWord[userSizePass];
int count = 0;
while (count < userPass[count]) {
testPassWord[count] = userPass[count];
count++;
}
isVerifyAccepted(testPassWord, userSizePass);
delete[] userPass;
userPass = NULL;
//system("pause");
return 0;
}
bool isVerifyAccepted(char *pass, int size){
bool verify[3];
if(size <= 6){
cout << "Password is too short "<<endl;
return false;
}
for(int i = 0; i<size; i++){
if(islower(pass[i])){
verify[0] = true;
break;
}else{
verify[0] = false;
}
}
for(int i = 0; i<size; i++){
if(isupper(pass[i])){
verify[1] = true;
break;
}else{
verify[1] = false;
}
}
for(int i = 0; i<size; i++){
if(isdigit(pass[i])){
verify[2] = true;
break;
}else{
verify[2] = false;
}
}
if(verify[0] == false){
cout << "You need at least one lowercase letter" << endl;
}
if(verify[1] == false){
cout << "You need at least one uppercase letter" << endl;
}
if(verify[2] == false){
cout << "You need at least one digit" << endl;
}
if((verify[0] == true) && (verify[1] == true) && (verify[2] == true)){
cout << "You have a good password, you met the criteria " <<endl;
}
return verify;
}
答案 0 :(得分:0)
因为根据调试器的这一行:const int SIZE = 6
。
将尺寸增加到32
或大于6
的某个数字。
有些密码超过6个字符。