"Bitflags" are pretty popular, you have perhaps had to use them even if you haven't implemented them yourself:
Code:
#include <iostream>
#include <string>
#define OK 0
#define TOO_SHORT 1
#define INVALID_CHAR 2
#define NO_CAP 4
using namespace std;
int validate_password (const string &pw) {
int rv = OK, i, cap = 0, len = pw.length();
if (len < 8) rv |= TOO_SHORT;
for (i = 0; i < len; i++) {
if (pw[i] < '0' || (pw[i] > '9' && pw[i] < 'A')
|| (pw[i] > 'Z' && pw[i] < 'a') || pw[i] > 'z') {
rv |= INVALID_CHAR;
continue;
}
if (pw[i] >= 'A' && pw[i] <= 'Z') cap++;
}
if (!cap) rv |= NO_CAP;
return rv;
}
int main(int argc, const char *argv[]) {
string eg;
cout << "Enter password: ";
cin >> eg;
int err = validate_password(eg);
cout << "Validating \"" << eg << "\"...\n";
if (err) {
if (err & TOO_SHORT) cout << "Password must be 8+ characters.\n";
if (err & INVALID_CHAR) cout << "Password must be only alphanumeric characters.\n";
if (err & NO_CAP) cout << "Password must contain a capital letter\n";
} else cout << "All good!\n";
return 0;
}
Notice that the values of the flags are exponential -- 1, 2, 4 -- and not sequential -- 1, 2, 3. If you don't know why that is, you should, so ask. Anyway, a single int using bitflags can be used to return upto 32 (1 per bit) different possibilities alone, and in combination:
Code:
[root~/C++] ./a.out
Enter password: asdf1234
Validating "asdf1234"...
Password must contain a capital letter
[root~/C++] ./a.out
Enter password: g56$
Validating "g56$"...
Password must be 8+ characters.
Password must be only alphanumeric characters.
Password must contain a capital letter