help with checking uppercase, lower, and number.

Write your question here.

I am trying to make a password verifier. When i try to check for an uppercase, lower case or number if does read it. I Think it reads the first letter. How can i make it read the whole array?
thanks in advance.



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
135
136
137
  #include <iostream>
#include <cstring>
#include <cctype>
#include <string>
using namespace std;

void validate(char *);
bool upper(char *, int size);
bool lower(char*, int size);
bool num(char*,int size);
bool length(char*);



int main()
{
	const int size = 30;
	char password[size];
	/*char *passPtr = password;*/
	cout << "Enter a password that is more " <<endl;
	cout <<  "than 6 characters " << endl;
	cout << "long, less than 20 characters and the password " << endl;
	cout << "must include an uppercase letter, a " << endl;
	cout << "lower case letter and a number." << endl;
	cin.getline(password, size);
	length(password);
	cout << password << endl;
	if (length(password) ==  false)
	{
		cout << "Password is either too long or does not contain enough characters." << endl; 
	}
	upper(password, size);
	if(upper(password, size) == false)
	{
		cout << "Password does not contain an uppercase letter." <<endl;
	} 
	lower(password, size);
	if(lower(password, size)== false)
	{
		cout << "Password does not contain a lower case letter." << endl;
	}
	num(password, size);
	if (num(password,size) == false)
	{
		cout << "Password does contain a number." << endl;
	}


	
	return 0;
}

bool length(char * password)
{
	int length;
	bool passlength = false;
    length = strlen(password);

		if (length <= 6 || length >= 20)
		{
			passlength = false;
			
		}
		else 
		{
			passlength = true;
			
		} 
		return passlength;

}

bool upper(char * password, int size)
{
	int length = strlen(password);
	bool up = true;
	for (int k = 0; k < length; k++)
	{
			if (isupper(password[k]))
			{
			up = true;
			
			}
			else
			up = false;
		
	
		
	}  
	

	return up;
}

bool lower(char * password, int size)
{
	int length = strlen(password);
	bool low = false;
	for (int j = 0; j < length; j++ )
	{
		if(islower(password[j]))
		{
			low = true;
			
		} 
		else
		{
			low = false;
			
		}
	}

	return low;
}

bool num(char * password, int size)
{
	int length = strlen(password);
	bool num = false;
	for (int i = 0; i < length; i++ )
	{
		if(isdigit(password[i]))
		{
			
			num = true;
			
			
		} 
		else
		{
		
			num = false;
			
		}
	}
	return num;
}
I would do it this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool upper(char * password, int size)
{
	int length = strlen(password);
	bool up = true;         //you don't need this. just make the whole function return a true or a false.
	for (int k = 0; k < length; k++)
	{
			if (isupper(password[k]))
			{
			return true; // once it hits a char that is in uppercase it breaks the loop (b/c there's no need to check other characters)            				             
	
                	}  
	
}


Try this and if it works then do the same with lowe and isDigit functions
You should use std::string not char*.

Consider 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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;

bool containsUpperCase(string str);
bool containsLowerCase(string str);
bool containsDigit(string str);

int main()
{
	string str("shadowC0DE");
	
	//check length
	if(str.length() < 6 || str.length() > 20)
	{
		cout<<"invalid password"<<endl;
		return 1;
	}
	bool upper,lower,digit;
	
	upper = containsUpperCase(str);
	lower = containsLowerCase(str);
	digit = containsDigit(str);
	
	cout<<str<<endl;
	cout<<"digit: "<<boolalpha<<digit<<endl;
	cout<<"upper: "<<boolalpha<<upper<<endl;
	cout<<"lower: "<<boolalpha<<lower<<endl;
	
	return 0;
}

bool containsUpperCase(string str)
{
	for(int i = 0; i < str.length(); i++)
		if(isupper(str[i]))
			return true;
	return false;
}
bool containsLowerCase(string str)
{
	for(int i = 0; i < str.length(); i++)
		if(islower(str[i]))
			return true;
	return false;
}
bool containsDigit(string str)
{
	for(int i = 0; i < str.length(); i++)
		if(isdigit(str[i]))
			return true;
	return false;
}
Last edited on
Thanks newbiee999. I got it working.
@shadowCODE, I am also trying your code too.
Topic archived. No new replies allowed.