I cant get my bool to call for true

Hey, I have tried everything. What am I doing wrong. I am trying to set up password input and my bool keeps reading wrong. I want to ensure that the password is at least 6char long and has a capitol, a lower, and a digit. Thanks for any help you can give. Here is my code

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 <cstring>
using namespace std;

const int SIZE = 80;
const int MIN = 6;
int isValid(char pwd[]);
int main()
{
	char password[SIZE];
	while (true)
	{
		cout << "Enter a Password: " << endl;
		cin.getline(password, SIZE);
		if (isValid(password) == 1)
		{
			cout << "Good Password!";
			break;
		}
		else
		{
			cout << "Password is invalid. Please try again"<<endl;
		}
	}
}
int isValid(char pwd[])
{
	bool minLength = false;
	bool hasUpper = false;
	bool hasLower = false;
	bool hasDigit = false;
	int length = strlen(pwd);
	if (length >= MIN)
	{
		minLength = true;
	}
	for (int i = 0; i < length; i++)
	{
		if (isupper(*pwd))
			hasUpper = true;
	}
	for (int i = 0; i < length; i++)
	{
		if (islower(*pwd))
			hasLower = true;
	}for (int i = 0; i < length; i++)
	{
		if (isdigit(*pwd))
			hasDigit = true;
		*pwd++;
	}
	if (minLength == true && hasUpper == true && hasLower == true && hasDigit == true)
	{
		return 1;
	}
	else

		return 0;
}
Last edited on
You have 3 for-loops using a variable i. Are you using that variable i? If not, why not? No need for pointers here.


Also, if hasUpper is already a bool ... then you don't need to ram home the point by hasUpper==true on line 52. Similarly for hasLower and hasDigit.
Last edited on
On lines 39 and 44 you're evaluating only the first character over and over again throughout the loop. I suspect you had the *pwd++; in each loop before but you removed it because your program was crashing. I suggest that you avoid modifying pointers to loop until you have more practice with the language, and just use the [] operator to access the elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int i = 0; i < length; i++)
{
	if (isupper(pwd[i]))
		hasUpper = true;
}
for (int i = 0; i < length; i++)
{
	if (islower(pwd[i]))
		hasLower = true;
}
for (int i = 0; i < length; i++)
{
	if (isdigit(pwd[i]))
		hasDigit = true;
}
Last edited on
This is for a school project that I have been working on and one of the requirements is that I use pointers but my bool are coming up as false, even when I changed it to the code listed by helios
Pointer math can be a bit tricky:
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
#include <iostream>

int main()
{
   // let's create an array
   int arr[] { 1, 2, 3, 4, 5 };

   // let's get the size of the array
   size_t size { sizeof(arr) / sizeof(arr[0]) };

   // get a pointer to the first array element
   int* arr_index { arr };

   for (size_t index { }; index < size; index++)
   {
      std::cout << *arr_index << ' ';
      arr_index++;
   }
   std::cout << '\n';

   // reset the pointer to the array's first element
   arr_index = &arr[0];

   for (size_t index { }; index < size; index++)
   {
      std::cout << *arr_index << ' ';
      arr_index++;
   }
   std::cout << '\n';

   // set the pointer to the array's last element
   arr_index = &arr[4];

   for (size_t index { }; index < size; index++)
   {
      std::cout << *arr_index << ' ';
      arr_index--;
   }
   std::cout << '\n';
}
1 2 3 4 5
1 2 3 4 5
5 4 3 2 1
Why are you doing a separate loop when checking for the conditions? Use one loop and you can check isupper, islower and isdigit.
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
#include <iostream>
#include <cstring>

const size_t MIN { 6 };

bool isValid(char pwd[]);

int main()
{
	const size_t SIZE { 80 };
	char password[SIZE];

	while (true)
	{
		std::cout << "Enter a Password: ";
		std::cin.getline(password, SIZE);

		if (isValid(password))
		{
			std::cout << "Good Password!\n";
			break;
		}
		else
		{
			std::cout << "Password is invalid. Please try again\n\n";
		}
	}
}

bool isValid(char pwd[])
{
	size_t length { strlen(pwd) };

	if (length < MIN)
	{
		return false;
	}

	bool hasUpper { false };
	bool hasLower { false };
	bool hasDigit { false };

	for (size_t i { }; i < length; i++)
	{
		if (isupper(*pwd)) { hasUpper = true; }

		if (islower(*pwd)) { hasLower = true; }

		if (isdigit(*pwd)) { hasDigit = true; }

		pwd++;
	}

	if (hasUpper && hasLower & hasDigit)
	{
		return true;
	}

	return false;
}
Enter a Password: arf
Password is invalid. Please try again

Enter a Password: arff125
Password is invalid. Please try again

Enter a Password: aRff125
Good Password!
Topic archived. No new replies allowed.