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
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;
}
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
#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';
}
#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)
{
returnfalse;
}
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)
{
returntrue;
}
returnfalse;
}
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!