I am trying to build a program that takes a user password (6+ characters, One uppercase, one lower case, and one number), and checks for errors. The idea is that if the user is doing something wrong (say, forgetting to use an uppercase letter), the program will tell them what the error is, and prompt them to enter the program again.
I get through the building process without errors, but whenever I run the program, I get this error:
I am new to C++, and am hoping somebody here with more experience can help me figure out why I am getting this error, because I have no clue! Here is my code:
#include <iostream>
#include <cctype>
usingnamespace std;
bool checkPass(char [], int); //Password Checking Function
char convert(string);
int main()
{
//Holds C-String Size
constint SIZE = 20;
char password[SIZE]; //Holds Password
bool check; //Variable for Password Check
char *pass; //Points to password
//Gets password from user
cout << "Please create a password (must contain upper case letter, lowercase letter, ";
cout << "and numumerican diget).\n" << endl;
cout << "Password: ";
cin.get(password, SIZE);
pass = password;
//Password runs through checkPass. If checkPass finds anything
//wrong, it tells the user what was wrong with the password,
//and allows them to enter a new one.
check = checkPass (pass, SIZE);
while (check = false){
cout << "Password: ";
cin.getline(password, SIZE);
check = checkPass(password, SIZE);
}
//Dispaying password for user
cout << "\nYour password is " << *pass << ".\n";
return 0;
}
bool checkPass (char pass[], int size)
{
int charCount = 0; //holds number of charcters for length check
int count;
//Gets number of characters, and checks for length
for (count = 0; count <= size; count++)
{
if (isprint(pass[count]))
{
charCount++;
}
}
if (charCount < 6)
{
cout << "Please use six or more characters\n" << endl;
returnfalse;
}
//Checks for Uppercase Letter
for (count = 0; count <= size; count++)
{
if (!isupper(pass[count]))
{
cout << "Please include at least one Uppercase letter\n" << endl;
returnfalse;
}
}
//Checks for Lowercase Letter
for (count = 0; count <= size; count++)
{
if (!islower(pass[count]))
{
cout << "Please include at least one Lowercase letter\n" << endl;
returnfalse;
}
}
//Checks for Number
for (count = 0; count <= size; count++)
{
if (!isdigit(pass[count]))
{
cout << "Please include at least one number\n" << endl;
returnfalse;
}
}
returntrue;
}
According to the box, the error appears on line 56 and 68. Thanks!
count should be < size, not <= size. In fact, you should check only the elements of pass that have actually been written.
Also, your loops after line 58 don't check what you think. They check, in turn, that pass has only upper case characters, that pass has only lower case characters, and that pass has only numbers.
#include <iostream>
#include <cctype>
usingnamespace std;
bool checkPass(char [], int); //Password Checking Function
char convert(string);
int main()
{
//Holds C-String Size
constint SIZE = 1000;
char password[SIZE]; //Holds Password
bool check; //Variable for Password Check
char *pass; //Points to password
//Gets password from user
cout << "Please create a password (must contain upper case letter, lowercase letter, ";
cout << "and numumerican diget).\n" << endl;
cout << "Password: ";
cin.get(password, SIZE);
pass = password;
//Password runs through checkPass. If checkPass finds anything
//wrong, it tells the user what was wrong with the password,
//and allows them to enter a new one.
check = checkPass (password, SIZE);
while (check = false){
cout << "Password: ";
cin.getline(password, SIZE);
check = checkPass(password, SIZE);
}
//Dispaying password for user
cout << "\nYour password is " << *pass << ".\n";
return 0;
}
bool checkPass (char pass[], int size)
{
int charCount = 0; //holds number of charcters for length check
int upperCount = 0;
int lowerCount = 0;
int numCount = 0;
int specCount = 0;
int count;
//Gets number of characters, and checks for length
for (count = 0; count < size; count++)
{
if (isprint(pass[count]))
{
charCount++;
}
}
if (charCount < 6)
{
cout << "Please use six or more characters\n" << endl;
returnfalse;
}
//Checks for Uppercase Letter
for (count = 0; count < charCount; count++)
{
if (isupper(pass[count]))
{
upperCount++;
}
}
//Checks for Lowercase Letter
for (count = 0; count < charCount; count++)
{
if (islower(pass[count]))
{
lowerCount++;
}
}
//Checks for Number
for (count = 0; count < charCount; count++)
{
if (isdigit(pass[count]))
{
numCount++;
}
}
//Sends back error if any are abscent
if(upperCount = 0)
{
cout << "INVALID: Please include at least 1 uppercase letter\n" << endl;
returnfalse;
}
elseif (lowerCount = 0)
{
cout << "INVALID: Please include at least 1 lowercase letter\n" << endl;
returnfalse;
}
elseif (numCount = 0)
{
cout << "INVALID: Please include at least 1 number\n" << endl;
returnfalse;
}
else
{
returntrue;
}
}
isprint() will also fail if you pass it characters that haven't been written. You don't even need it. The end of the string is marked by a zero. Not a '0', a 0; (char)0.
You need to break the loop when you find it. Even better, just use strlen() or strnlen().
This appears to be a really silly bug in the Microsoft offering.
(It happens only with 'Start Debugging' from the IDE when there is no breakpoint at the line in question.)
Do something like:
1 2 3
// isprint(pass[count])
unsignedchar c = pass[count] ;
isprint(c) ;
#include <iostream>
#include <cctype>
bool checkPass( constchar[], int ) ; // make it const-correct.
int main()
{
constint SIZE = 20;
char password[SIZE]; //Holds Password
//Gets password from user
std::cout << "Please create a password (must contain upper case letter, lowercase letter, ";
std::cout << "and numumerican diget).\n" << '\n';
//Password runs through checkPass. If checkPass finds anything
//wrong, it tells the user what was wrong with the password,
//and allows them to enter a new one.
bool check = false ;
while( !check )
{
std::cout << "Password: ";
std::cin.getline( password, SIZE );
// std::cin.gcount() gives us the number of characters that were read by std::cin.get()
int num_chars_read = std::cin.gcount() ;
check = checkPass( password, num_chars_read );
}
//Dispaying password for user
std::cout << "\nYour password is " << password << ".\n";
}
bool checkPass( constchar pass[], int nchars )
{
int n_printable = 0, n_uppercase = 0, n_lowercase = 0, n_digits = 0 ;
for( int count = 0; count <= nchars ; ++count )
{
//***********************************************
unsignedchar c = pass[count] ;
//***********************************************
if( std::isprint( c ) )
{
++n_printable ;
if( std::isupper( c ) ) ++n_uppercase ;
elseif( std::islower( c ) ) ++n_lowercase ;
elseif( std::isdigit( c ) ) ++n_digits ;
}
}
if( n_printable < 6 )
{
std::cout << "Please use six or more printable characters\n" << '\n';
returnfalse;
}
//Checks for Uppercase Letter
if( n_uppercase == 0 )
{
std::cout << "Please include at least one Uppercase letter\n" << '\n';
returnfalse;
}
//Checks for Lowercase Letter
if( n_lowercase == 0 )
{
std::cout << "Please include at least one Lowercase letter\n" << '\n';
returnfalse;
}
//Checks for Number
if( n_digits == 0 )
{
std::cout << "Please include at least one digit\n" << '\n';
returnfalse;
}
returntrue;
}