Skip to content

Commit

Permalink
Update validation.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Josseye authored Feb 15, 2024
1 parent 9a1753b commit 361c595
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions homework/password-check/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
#include <iostream>
#include <string>
#include <cctype>

enum class Error {
enum class ErrorCode {
Ok,
PasswordNeedsAtLeastNineCharacters,
PasswordNeedsAtLeastOneNumber,
PasswordNeedsAtLeastOneSpecialCharacter,
PasswordNeedsAtLeastOneUppercaseLetter,
PasswordsDoNotMatch
};
};

std::string getErrorMessage(int errcode) {
switch (errcode) {
case 0:
return "Ok";
case 1:
return "Password Needs AtLeast Nine Characters";
case 2:
return "Password Needs AtLeast OneNumber";
case 3:
return "Password Needs AtLeast One Special Character";
case 4:
return "Password Needs AtLeast One Uppercase Letter";
case 5:
return "Passwords Do Not Match";
case 0:
return "Ok";
case 1:
return "Password Needs AtLeast Nine Characters";
case 2:
return "Password Needs AtLeast OneNumber";
case 3:
return "Password Needs AtLeast One Special Character";
case 4:
return "Password Needs AtLeast One Uppercase Letter";
case 5:
return "Passwords Do Not Match";
}
}

Expand All @@ -34,38 +34,39 @@ bool doPasswordsMatch(std::string psw1, std::string psw2) {
return true;
else
return false;
}
}

int checkPasswordRules(std::string pass) {
int i;
Error back;
ErrorCode back;
bool OneNumber, OneUppercaseLetter, OneSpecialCharacter;
OneNumber = false;
OneUppercaseLetter = false;
OneSpecialCharacter = false;
if (pass.length()<9)
return static_cast <int>(back = Error::PasswordNeedsAtLeastNineCharacters);
return static_cast <int>(back = ErrorCode::PasswordNeedsAtLeastNineCharacters);
for (auto element : pass) {
if (isdigit(element))
OneNumber = true;
if (!isalnum(element))
OneSpecialCharacter = true;
if (isupper(element))
OneUppercaseLetter = true;
}
}
if (!OneNumber)
return static_cast <int>(back = Error::PasswordNeedsAtLeastOneNumber);
return static_cast<int>(back = ErrorCode::PasswordNeedsAtLeastOneNumber);
else
if (!OneUppercaseLetter)
return static_cast <int>(back = Error::PasswordNeedsAtLeastOneUppercaseLetter);
return static_cast<int>(back = ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter);
else
if (!OneSpecialCharacter)
return static_cast <int>(back = Error::PasswordNeedsAtLeastOneSpecialCharacter);
else return static_cast <int>(back = Error::Ok);
return static_cast <int>(back = ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter);
else return static_cast <int>(back = ErrorCode::Ok);
}

int checkPassword(std::string firstpassword, std::string secondpassword) {
if (!doPasswordsMatch(firstpassword, secondpassword))
return static_cast<int>(Error::PasswordsDoNotMatch);
return static_cast<int>(ErrorCode::PasswordsDoNotMatch);
else
return checkPasswordRules(firstpassword);
}

0 comments on commit 361c595

Please sign in to comment.