C++ 堆栈溢出运行时错误c++;

C++ 堆栈溢出运行时错误c++;,c++,C++,我正在为一个密码验证器类编写一个程序。我需要的密码至少是6个字符,但我也限制它最多10个(不是必需的,但想) 在我尝试添加一个超过10个字符的密码之前,这个程序工作得很好,我的其他验证工作也很好。我可以通过并重新输入一个新密码,我得到的信息是,它现在是一个有效的密码。但是我得到了一个关于以下错误的答案: 运行时检查失败#2-变量“password”周围的堆栈已损坏 如果我设置宽度,我可以绕过错误,但我知道这不是正确的处理方法。我还认为,通过设置宽度,用户将无法输入除该大小以外的任何内容 这是我的

我正在为一个密码验证器类编写一个程序。我需要的密码至少是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;
 }
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
布尔验证密码(字符*)//测试pw要求的功能
bool-validateUppercase(char*)//用于测试大写值的函数
bool-validateLowercase(char*)//用于测试小写值的函数
布尔验证枚举(字符*)//用于测试数字值的函数
布尔验证长度(字符*)//用于测试密码长度的函数
int main()
{
//变化无常
字符密码[11];//设置密码的最大字符数,包括结束分隔符
int size;//数组的大小
//根据条件提示用户输入密码
cout(10);
if(验证密码(密码))
{

cout使用cstrings,运算符
使用cstrings,运算符
从外观上看,将变量声明从
字符密码[11]<代码> >代码> STD::字符串密码。你可以很好的去执行。<代码> char < /Cuff>缓冲区可能太短了,无法保存数据。不要混合C和C++。SoMeTeimes你有C char *,有时STD::String。这个电影没有快乐的结局。根据它的外观,从< Calth> Char密码[1]中改变变量声明。<代码> >代码> STD::字符串密码< /C> >,你可以很好地去。“代码”> char < /Cuff>缓冲区可能太短,无法保存数据。不要混合C和C++。SoMeTeMes你有C char *,有时STD:::String。这个电影没有快乐的结局。我回顾了你的链接,但我不确定我理解它。我对C++很陌生。所以当我改变字符PAS时剑[10]到字符串密码;我在每次引用密码时都会出错。我不知道如何修复。现在,您只需通过
char password[50]
更改
char password[11]
。如果您想使用
string
,您还需要更改方法的签名,即替换
validateXX(char*pass)
by
validateXX(字符串传递)
。请不要忘记将问题标记为已回答(选中左侧的复选标记)如果答案适合您,欢迎使用stackoverflow!非常感谢!!!现在我正在增加大小,因为如果大于10,我将进行验证。但我将尝试将其更改为string
validateXX(const string&pass)
通过引用获取字符串,但不允许
validateXX()
修改它。
validateXX(string&pass)
通过引用获取字符串,并让
validateXX()修改它。
validateXX(string pass)
通过复制原始字符串,通过值获取字符串;
validateXX()可以修改字符串的副本,而不改变原来的字符串。我查看了你的链接,但我不确定我是否理解它。我对C++很陌生。所以当我把字符密码[10 ]改为字符串密码时,每次密码的引用都会出错。我不知道如何修复。现在,你可以简单地更改<代码>字符密码[11 ]。
by
char password[50]
。如果您想使用
string
,您还需要更改方法的签名,即用
validateXX(char*pass)
替换
validateXX(string pass)
。请不要忘记将问题标记为已回答(选中左侧的标记)如果答案适合您,欢迎使用stackoverflow!非常感谢!!!现在我正在增加大小,因为如果大于10,我将进行验证。但我将尝试将其更改为string
validateXX(const string&pass)
通过引用获取字符串,但不允许
validateXX()
修改它。
validateXX(string&pass)
通过引用获取字符串,并让
validateXX()
修改它。
validateXX(string pass)
通过复制原始字符串按值获取字符串;
validateXX()
可以修改其字符串副本而不更改原始字符串。
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";