I am currently coding a program that checks a password to see if it is valid. The password must be 7 character and have an uppercase and lowercase letter. My current code can reach the first check - length - but then has a debug assertion failure. I believe that I need to fix the initial strings, but am not sure. Any help would be greatly appreciated.
// Lab 9
// coded by Elijah
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <cstring>
using namespace std;
string checkCharacter(string);
string checkCharacterTwo(string);
int main()
{
string capitalAnswer = "O", capitalAnswerTwo = "O";
std::string password;
cout << "Please enter a password: ";
cin >> password;
if (password.length() >= 7)
{
capitalAnswer = checkCharacter(password);
if (capitalAnswer == "y")
{
string checkCharacterTwo(password);
if (capitalAnswerTwo == "y")
{
cout << "Thank you, that is a valid password" << endl;
return 0;
}
else
{
cout << "Passwords must contain at least one lowercase letter" << endl;
main();
}
}
else
{
cout << "Passwords must include at least one uppercase letter" << endl;
main();
}
}
else
{
cout << "Passwords must be at least 7 characters long" << endl;
main();
}
}
string checkCharacter(string password)
{
string answer;
string character;
string capitalAnswer;
int number;
for (number = 0; number > password.length() || answer != "y"; number++)
{
character = password[number];
if (character >= "A" && character <= "Z")
answer = "y";
else
answer = "n";
}
{
if (answer == "y")
capitalAnswer = "y";
else
capitalAnswer = "n";
}
return capitalAnswer;
}
string checkCharacterTwo(string password)
{
string answer;
string character;
string capitalAnswerTwo;
for (int number = 0; number > password.length() || answer != "y"; number++)
{
character = password[number];
if (character >= "a" && character <= "z")
answer = "y";
else
answer = "n";
}
if (answer == "y")
capitalAnswerTwo = "y";
else
capitalAnswerTwo = "n";
return capitalAnswerTwo;
}
It appears that your looping condition in checkCharacter()
is wrong and makes you go out of bounds. Correct it as foolows:
for (number = 0; number < password.length() && answer != "y"; number++)
The same problem appears in checkCharacterTwo()
, that should become:
for (int number = 0; number < password.length() && answer != "y"; number++)
Finally, you don't set the value of capitalAnswerTwo
. You have to replace:
string checkCharacterTwo(password);
with:
capitalAnswerTwo = checkCharacterTwo(password);
Then everything is fine !