Im getting runtime error code #2 buffer overrun from pword char array. Any tips to help resolve isuue.
//Write a program to determine whether or not an entered
//password is valid.Valid passwords consist of 10 characters,
//4 of which must be letters and the other 6 digits.
//The letters and digits can be arranged in any order.
//Only lowercase letters are allowed for valid passwords.
//Hints: use islower, isalpha, isdigit, and strlen.
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
const int Size = 10;
char pword[Size];
int count=0;
int totall = 0;
int totaln = 0;
bool state = NULL;
while(state != true)
{
cout << "Enter a password up to 10 characters: " << endl;
cin >> pword;
int psize = strlen(pword);
totall = 0;
totaln = 0;
while (pword[count] != '\0')
{
if (isalpha(pword[count]))
{
totall += 1;
}
if (isdigit(pword[count]))
{
totaln += 1;
}
count++;
}
if (totall > 4 || totaln > 6)
{
cout << " Error! Only enter 4 characters and 6 digits." << endl;
state = false;
}
else
{
state = true;
}
count = 0;
while(pword[count]!='\0')
{
if (!islower(pword[count]))
{
if (!isdigit(pword[count]))
{
cout << "Error all characters must be lowercase!" << endl;
state = false;
}
}
count++;
}
}
cout << "The password you entered is: " << pword << endl;
cout << "The number of lower case letters in the password is " << totall << endl;
cout << "The number of digits in the password is " << totaln << endl;
return 0;
}
I tried dynamically allocating the char array by :
pword[Size]= (char*)malloc(sizeof(char))={};
failed though so I reverted the code back. I'm attempting to make a space on the heap for the char array indices 0-10 with null terminating character at 10 index. Looking at malloc though I don't see how I can make the array longer than a char this way?
The exact error message is "Run-Time Check Failure #2 - Stack around the variable 'pword' was corrupted." it occurs only when an incorrect password is entered then at completion the error occurs.
The main fix is to change this declaration:
int pword[Size];
To simply this:
string pword;
No more buffer overrun possibilities. Also bool state = NULL;
doesn't sound like something that should compile, but perhaps the NULL coerces to false
.
Some minor improvements to the code:
int main()
{
string pword;
int totall = 0;
int totaln = 0;
int isAllLowercase = true;
bool state = false;
while(state != true)
{
cout << "Enter a password up to 10 characters: " << endl;
cin >> pword;
totall = 0;
totaln = 0;
isAllLowercase = true;
for (char c : pword)
{
if (isalpha(c))
{
totall += 1;
}
if (isdigit(c))
{
totaln += 1;
}
isAllLowerCase = isAllLowerCase && (islower(c) || isdigit(c));
}
if (totall > 4 || totaln > 6)
{
cout << " Error! Only enter 4 characters and 6 digits." << endl;
}
else if (!isAllLowerCase)
{
cout << "Error all characters must be lowercase!" << endl;
}
else
{
state = true;
}
}
cout << "The password you entered is: " << pword << endl;
cout << "The number of lower case letters in the password is " << totall << endl;
cout << "The number of digits in the password is " << totaln << endl;
return 0;
}