我正在为密码验证器编写类程序。我需要密码至少为6个字符,但我也将密码限制为最多10个(不是必需的,但希望如此)。

该程序运行良好,其他验证也有效,直到尝试添加超过10个字符的密码为止。我可以浏览并重新输入一个新密码,然后我收到一条消息,提示它现在是有效密码。但随后出现以下错误:


  运行时检查失败#2-变量'password'周围的堆栈已损坏。


如果设置宽度,我可以解决该错误,但是我知道这不是正确的处理方式。另外,我认为通过设置宽度,用户将无法输入该大小以外的任何内容。

这是我的代码:

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>

using namespace std;

//Function prototypes
bool validatePassword(char *);                      //function to test pw requirements
bool validateUppercase(char *);                 //function to test for upper case value
bool validateLowercase(char *);                 //function to test for lowercase value
bool validateNumber(char *);                    //function to test for a number value
bool validateLength(char *);                    //function to test for password length

int main()
{
    //Variabes
    char password[11];                              //set the maximum number of char for password include the end     delimiter
    int size;                                       //size of the array

    //Prompt user to enter a password based on criteria

    cout << "Please enter a password that meets the following criteria: \n";
    cout << "1. A minimum of 6 characters up to a max of 10. \n";
    cout << "2. Contains atleast one uppercase and one lowercase character. \n";
    cout << "3. Contains atleast one number. \n\n";
    //cout << "****Please note if you have more the 10 characters \n";
    //cout << " the program will only accept the first 10***\n";
    cout << "\nPlease enter your password: ";
    cin >> password;
    //cin  >> setw(10) >> password;             //set this so I would not get a runtime error

    size = strlen(password);                        //determines the length of the password

    do
    {
        while (!validatePassword(password))     //if the functions returns a false value
        {
            cout << "\nYour password does not meet the requirements. ";
            cout << "\nEnter a new password: ";
            cin >> password;
            size = strlen(password);
        }
    } while (size > 10);

    if (validatePassword(password))
    {
        cout << "\nYour password " << password << " meets the requirements. \n";    //if the function returns a true value

    }

    system ("pause");

    return 0;

}

//This function calls the other validation functions
bool validatePassword(char *pass)
{
int size = strlen(pass);
return (validateLength(pass) && validateUppercase(pass) &&         validateLowercase(pass) && validateNumber(pass) == true);
}

//This function validates the length of the password
bool validateLength (char *pass)
{
    int size = strlen(pass);
    if (size >= 6 && size < 10)
        return true;
    else
    {
        cout << "\n\nThe password you entered either contained to little or to many characters.";
        cout << "\nA minimum of 6 characters to a maximum of 10 is required.";
        return false;
    }
}

//This function checks to see if the password contains an uppercase char
bool validateUppercase(char *pass)
{
    int size = strlen(pass);
    for (int count = 0; count < size; count++)
    {
        if (isupper(pass[count]))
            return true;
    }

    cout << "\n\nThe password must contain at least one uppercase  character. ";
    return false;
}

 //This function checks to see if the password contains an lowercase char
 bool validateLowercase(char *pass)
 {
    int size = strlen(pass);
    for (int count = 0; count < size; count++)
    {
        if (islower(pass[count]))
            return true;
    }

    cout << "\n\nThe password must contain at least one lowercase character. ";
    return false;
 }

//This function checks to see if the password contains an number char
bool validateNumber(char *pass)
{
    int size = strlen(pass);
    for (int count = 0; count < size; count++)
    {
        if (isdigit(pass[count]))
            return true;
    }

    cout << "\n\nThe password must contain at least one number. " ;
    return false;
 }

最佳答案

使用cstrings时,如果有足够的空间,运算符<<将读取一个字符串并在缓冲区的末尾自动添加空字节字符。

您将缓冲区分配为11,因此当输入的密码大于10时,将不存在终止字符串的空字节,从而导致strlen等问题。实际上,strlen所做的只是读取输入并递增计数器,直到遇到\0

您现在有两个选择:


继续使用cstring并增加缓冲区的大小(足够大以避免发生意外),
切换到c ++的string类,该类是可以动态增加的char数组(强烈建议here is a link to get started)。




另外,关于代码:您有很多不必要的东西。例如:

do
{
    while (!validatePassword(password))     //if the functions returns a false value
    {
        cout << "\nYour password does not meet the requirements. ";
        cout << "\nEnter a new password: ";
        cin >> password;
        size = strlen(password);
    }
} while (size > 10);

if (validatePassword(password))
{
    cout << "\nYour password " << password << " meets the requirements. \n";    //if the function returns a true value

}


可以替换为:

while (!validatePassword(password))
{
   cout << "\nYour password does not meet the requirements. ";
   cout << "\nEnter a new password: ";
   cin >> password;
}
cout << "\nYour password " << password << " meets the requirements. \n";


validatePassword调用validateLength来检查strlen(pass) <= 10,并且只有在密码正确时才能退出第一个循环。

另一个改进是将密码的大小传递给函数,以避免在任何地方调用strlen(在C语言中,总是使用char *参数传递该大小是很常见的)。

关于c++ - 堆栈溢出运行时错误C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41796006/

10-13 08:15