我的程序有时会崩溃,原因是C++

What does my program crash sometimes C++?

本文关键字:C++ 崩溃 程序 我的      更新时间:2023-10-16

所以我要创建一个程序来验证密码是否正确创建。要通过验证,用户必须输入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 passwordna good password has to be at least six characters longn" 
     << "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;     
}

因为这一行根据调试器:const int SIZE = 6

将大小增加到32或大于6的某个数字。某些密码的长度超过6个字符。