Password program

Okie dokie I have to write a program that takes a password and makes sure it has 8 characters, 1 capital, and 1 lowercase letter.

Now I know I'm close. I can get two or three different passwords to work and than some just don't. Any help is appreciated! Please explain with suggestions, my professor is not that great. Thank you!


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134



#include <iostream>
#include <cctype>
#include <string>

using namespace std;


int main()
{


	const int length = 8;
	int password[length];

	cout << "Please enter a password: " << endl;
	cin >> password[length];




	{


		int i = 0;
		int big = 0; // counter for big
		int lil = 0; // counter for little
		bool valid = false;

		while (i > password[length])

		{

			if (password[length] >= 'a' && password[length] <= 'z')

			{

				lil++;

			}
			else
			{
				
				if (password[length] >= 'A' && password[length] <= 'Z')
				{



				}

				else
				{

					big++;

				}

			}
			

			i++;
		}








		if (8 >= password[length])
		{
			
		}
		else
		{

			cout << "You need to have at least 8 characters in your password." << endl;

		}

		if (big == password[length])
		{
			
			cout << "You need at least one Uppercase letter." << endl;

		}

		else
		{

			
			if (lil == password[length])
			{

				cout << "You need to have at least one Lowercase letter." << endl;
				
			}
			else
			{

				

			}

		}
		
		if (valid = password[length])
		{

			

		}
		else
		{

			cout << "Thank you, that is a valid password!" << endl;

		}
	}

		




		system("pause");
		return 0;

	
}
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
#include <iostream>
#include <string> // https://cal-linux.com/tutorials/strings.html
#include <cctype> // http://en.cppreference.com/w/cpp/header/cctype

int num_alpha_chars( std::string str )
{
    int num_alpha = 0 ;
    for( char c : str ) if( std::isalpha(c) ) ++num_alpha ;
    return num_alpha ;
}

bool has_lower_case_char( std::string str )
{
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char c : str ) if( std::islower(c) ) return true ;
    return false ;
}

bool has_upper_case_char( std::string str )
{
    for( char c : str ) if( std::isupper(c) ) return true ;
    return false ;
}

int main()
{
    std::string pword ;
    std::cout << "password? " ;
    std::getline( std::cin, pword ) ; // or std::cin >> pword if spaces are not allowed

    if( num_alpha_chars(pword) < 8 ) std::cout << "at least 8 alpha characters are required.\n" ;
    else if( !has_lower_case_char(pword) ) std::cout << "there must be at least one lower case character\n" ;
    else if( !has_upper_case_char(pword) ) std::cout << "there must be at least one upper case character\n" ;
    else std::cout << "that is a valid password\n" ;
}
there are some logic errors in your code. this version works
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
int main()
{
    const int PASSWORD_MAX = 1024;
    char password[PASSWORD_MAX];
    cout << "Please enter a password: " << endl;
    std::cin.getline(password, sizeof(password));
    {
        int i = 0;
        int big = 0; // counter for big
        int lil = 0; // counter for little
        bool valid = true;

        for (int i = 0; i < strnlen(password, sizeof(password)); i++)
        {
            if (password[i] >= 'a' && password[i] <= 'z')
            {
                lil++;
            }
            else if (password[i] >= 'A' && password[i] <= 'Z')
            {
                big++;
            }
        }

        if (8 > strnlen(password, sizeof(password)))
        {
            cout << "You need to have at least 8 characters in your password." << endl;
            valid = false;
        }

        if (big == 0)
        {
            cout << "You need at least one Uppercase letter." << endl;
            valid = false;
        }

        if (lil == 0)
        {
            cout << "You need to have at least one Lowercase letter." << endl;
            valid = false;
        }

        if (valid)
        {
            cout << "Thank you, that is a valid password!" << endl;
        }
        else
        {
            cout << "invalid password!" << endl;
        }
    }
    system("pause");
    return 0;
}


this is my version
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
bool check_password(const std::string password)
{
    // make sure it has at lease 8 characters
    if (password.size() < 8)
    {
        std::cout << "you need to have at lease 8 charaters" << std::endl;
        return false;
    }

    int numOfCapital = 0;
    int numOfLowerCase = 0;

    for (char c : password)
    {
        if (c >= 'a' && c <= 'z')
        {
            numOfLowerCase++;
        }

        if (c >= 'A' && c <= 'Z')
        {
            numOfCapital++;
        }
    }

    // make sure it has at lease one capital
    if (numOfCapital < 1)
    {
        std::cout << "you need to have at lease one capital letter" << std::endl;
        return false;
    }

    // make sure it has at least one lowercase
    if (numOfLowerCase < 1)
    {
        std::cout << "you need to have at least one lower case letter" << std::endl;
        return false;
    }

    return true;
}

int main()
{
    char password[256];   //8 characters, plus '\0' for teminating
    std::cin.getline(password, sizeof(password));
    bool valid = check_password(std::string(password));

    if (valid)
    {
        std::cout << "password is valid" << std::endl;
    }
    else
    {
        std::cout << "password is invalid!" << std::endl;
    }

    system("pause");
    return 0;
}
Thank y'all so much for the help! Definitely had a few hunches about some of the code and should have acted on them, because they turned out right!
Topic archived. No new replies allowed.