I came up with this solution with some help regarding the CS50 Week 2 Problem set password. The prompt is to use 2 function to come up with a simple password checker, verifying an input string has 1 uppercase letter, 1 lowercase letter, 1 number, and 1 symbol. The suggestion was to use bools. However, I have 3 questions on how and why this works:
- Bools... I still am struggling with them. Why don't I have to put the =true part? Are bools just assumed to have to be true?
- Doing some research, I understand now that the last statement in an
ifclause doesn't have to beelse, because that can't be specified. But why does oneifand threeelse ifwork? I originally anticipated that it would be a sort of nestedifstatements instead. - Does the bool valid(string password) function take false and true as input because it's defined as using bool?
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
bool valid(string password);
int main(void)
{
string password = get_string("Enter your password: ");
if (valid(password))
{
printf("Your password is valid!\n");
}
else
{
printf("Your password needs at least one uppercase letter, lowercase letter, number and symbol\n");
}
}
bool valid(string password)
{
bool haslower = false;
bool hasupper = false;
bool hasdigit = false;
bool haspunct = false;
int i;
for (i = 0; password[i] != '\0'; i++)
{
if (isupper(password[i]))
{
hasupper = true;
}
else if (islower(password[i]))
{
haslower = true;
}
else if (isdigit(password[i]))
{
hasdigit = true;
}
else if (ispunct(password[i]))
{
haspunct = true;
}
}
if (hasupper && haslower && hasdigit && haspunct)
{
return true;
}
return false;
}
Bool or boolean is a primitive data type. There are only two possible values, true or false, which can be denoted as 1 or 0. Booleans are used to denote whether something is true or not, yer or no, and in this case, either it is a valid password or its not. Google more about this.
end? or else? if statements have the option to be chained together using elseif where the predicate is checked through several conditional statements. For example, lets say the predicate is age. If you "age < 21 no alcholo, else yes alcohol" In this case you only need 2. What if you want to check whether a student qualifies for an award and you must check several conditions to see if each are met. If statements aren't required to be chained but they can very well be if needed. They can be nested within other ifs, loops as well and much more. Google what you don't understand and gravitate towards official documentation after a while.
No, Bool is the return type. The function either returns true or false. The parameter takes in a string. So it takes in a set of characters and in this case a password.
You can call the function, or invoke the function in main or any other user defined function by calling the function valid and passing in a string argument. The argument MUST be a string not a bool. The function determines whether the password is indeed a valid password based on the conditional requirements you specified in your if statements. I'm surprised you wrote this but don't understand it but don't give up. Feel free to ask more questions. Be more specific about the lines you refer to next time as well!
Are you watching the shorts from cs50 provided by Doug? They are very useful in helping explain the material further.
Again, lots of jargon here, but google words and concepts you don't understand.
Best of luck!