sizeof(pass) always returns 4 - it's returning the size in bytes of the pointer that points to the string, not the actual length of the string itself.. if you change string pass; to char pass[100]; and all occurances of sizeof(pass) to strlen(pass) You'll get code that correctly counts the number of letters in the password. The logic in your code then gets a bit funky, and I can't quite tell what you're trying to do there. Often things become much more clear when you separate out the individual parts of a problem into their own functions. Might I suggest a different approach to this problem?
#include <iostream>
using namespace std;
int countNumChars(char *str)
{
//
// TODO: add code for this function
//
//
}
int countLowerChars(char *str)
{
//
// TODO: add code for this function
//
//
}
int countUpperChars(char *str)
{
//
// TODO: add code for this function
//
//
}
bool isValidPassword(char *password)
{
int len, numberChars, lowerCaseChars, upperCaseChars;
bool result = false;
len = strlen(password);
numberChars = countNumChars(password);
lowerCaseChars = countLowerChars(password);
upperCaseChars = countUpperChars(password);
result = ((len==10)&&(lowerCaseChars>=1)&&(upperCaseChars>=2)&&(numberChars>=1));
return result;
}
int main()
{
char pass[100];
cout << "Please enter a 10 character password.\\n";
cout << "You must make sure your password has at\\n";
cout << "least two uppercase and at least one\\n";
cout << "lowercase letter and at least 1 number.\\n";
cin >> pass;
while (!isValidPassword(pass))
{
cout << "Invalid Password. Please type again.\\n";
cin >> pass;
}
cout << "Your password: " << pass << ", is good and accepted.\\n";
system("pause");
return 0;
}