IS VALID FUNCTION

I need some help on a few things.


Include a function named isValidPassword that will complete all the checks and return a Boolean value which will indicate whether the password is valid or not.
for this i am thinking i would remove my test in main and that will become my function definition but for the prototype would it return a int or a bool. I never got that good at functions and kind of get overwhelmed on where to draw the line or which way to follow.


second,

the additional requirement that the password contain at least one special character. assume that any character that is printable, not alphabetic, not numeric, and not a space character is "special". That includes punctuation, but may include additional non-punctuation characters. what would be the best way for something like this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 #include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
	const int LENGTH = 101;
	char List[LENGTH];
	int Upper, Lower, Digit;

	// Ask user to create a password
	cout << "Create your password!\n"

		<< "_____________________________________________\n"

		<< "Passwords must meet the following criteria:\n" <<

		   "_____________________________________________\n"

		<< "- The password must have at least six characters long.\n"

		<< "- The password must have at least one uppercase letter.\n"

		<< "- The password must have at least one lowercase letter.\n"

		<< "- The password must have at least one digit.\n";

	do
	{
		Upper = Lower = Digit = 0;

		cout << endl << " Enter password: ";
		cin.getline(List, LENGTH);

		for (int i = 0; i < strlen(List); i++)
		{
			if (isupper(List[i]))
				Upper++;
			if (islower(List[i]))
				Lower++;
			if (isdigit(List[i]))
				Digit++;
		}

		if (strlen(List) < 12)
			cout << "Password is not at least twelve characters long.\n";
		if (Upper == 0)
			cout << "Password needs to have at least one uppercase letter.\n";
		if (Lower == 0)
			cout << "Password needs to have at least one lowercase letter.\n";
		if (Digit == 0)
			cout << "Password needs to have at least one digit.\n";
	} while (Upper == 0 || Lower == 0 || Digit == 0 || strlen(List) < 6);

	cout << " Password verified." << endl << " Your password is: " << List << endl;

	return 0;
}
For the first part, your code will look something like:
1
2
3
4
main:
    do
        read password
    while !isValidPassword


And the check function will perform all the checks you mention in the code.

The second part is just adding further checks.
currently i got the second part down but am still struggling with the proper function use for isvalid.
This is what im trying but currently failing. I attempt what you suggest.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92


#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;


//
bool isValidPassword(string list);

int main()
{
	

	if (password = true) {

		cout << " Password verified." << endl << " Your password is: " << List << endl;
	}
	return 0;
}

bool isValidPassword(string password) {


	const int LENGTH = 101;
	char List[LENGTH];
	int Upper, Lower, Digit, Special;



	cout << "Create your password!\n"

		<< "_____________________________________________\n"

		<< "Passwords must meet the following criteria:\n" <<

		"_____________________________________________\n"

		<< "- The password must have at least Tweleve characters.      ||\n"

		<< "- The password must have at least one Uppercase letter.    ||\n"

		<< "- The password must have at least one Lowercase letter.    ||\n"

		<< "- The password must have at least one Digit.               ||\n"

		<< "- The password must have at least one Special character.   ||\n";



	do
	{
		Upper = Lower = Digit = Special = 0;

		cout << endl << " Enter password: ";
		cin.getline(List, LENGTH);

		for (int i = 0; i < strlen(List); i++)
		{
			if (isupper(List[i]))
				Upper++;
			if (islower(List[i]))
				Lower++;
			if (isdigit(List[i]))
				Digit++;
			if (ispunct(List[i]))
				Special++;
		}

		if (strlen(List) < 12)
			cout << "Password needs to have at least twelve characters.        ||\n";
		if (Upper == 0)
			cout << "Password needs to have at least one uppercase letter.     ||\n";
		if (Lower == 0)
			cout << "Password needs to have at least one lowercase letter.     ||\n";
		if (Digit == 0)
			cout << "Password needs to have at least one digit.                ||\n";
		if (Special == 0)
			cout << "Password needs to have at least one special character.    ||\n";
	} while (Upper == 0 || Lower == 0 || Digit == 0 || Special == 0 || strlen(List) < 12);







}


completely lost i broke everything!
the second part, make a lookup table of the ascii table if its ascii. mark the letters and digits and such and unprintables, anything else is fair game.

if its Unicode, you are in trouble!


i got the second part down @jonnin but i just cant make it into a function properly that does what i want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

bool isValidPassword(string password);

int main()
{   
    std::string password;

    do {
        // read password from user
    }
    while (!isValidPassword(password));
}

bool isValidPassword(string password) {
    bool ok = true;
        
    // Passwords must meet the following criteria: -- you need to implement them

    // The password must have at least Tweleve characters.
    if (ok) {
        ok = password.size() >= 12;
    }
        
    // The password must have at least one Uppercase letter.
    if (ok) {
        ok = false;
        for (size_t i = 0; !ok && i != password.size(); ++i)
            ok = isupper(password[i]);
    }
        
    //  The password must have at least one Lowercase letter.

    //  The password must have at least one Digit.

    //  The password must have at least one Special character.

    return ok;
}
Topic archived. No new replies allowed.