c++ - Password Checking Program - Matching Password Failure - Looping Failure -


as title states, program users input password. program checks 4 rules password pass (1). prompts user enter password again validation, program compares initial password (2) , states if 2 passwords match , acceptable.

(1) - first rule, checking string length (must @ least 8 characters long), loop appears state failed regardless of if did or not. said, if did fail, program makes user enter in password until user enters in password passes rule.

example: if enter in "password", it'll output prompt entered incorrectly, continue rest of program if enter correctly second time.

(2) can't seem correctly compare 2 strings check accuracy , have tried several techniques see if change. many ways resulted in while loop cancelling out entirely.

here code: (i appreciate , comes way, been working on program long time now, no avail)

/*this program determines if entered password valid , if second password entry matches first. */  #include <iostream>  #include <cstring>  #include <string.h>  #include <string>  using namespace std;  void userinput(string password); short passwordalphanumcheck(string password); void passwordvalidationoutput(string password); void passwordmatch(string password, string second_password);  int main() {     string password;     string second_password;      userinput(password);     passwordvalidationoutput(password);     passwordmatch(password, second_password);      return 0; }  void userinput(string password) {     cout << "the password entering must: " << endl;     cout << " " << endl;     cout << "1) @ least 8 characters long" << endl;     cout << "2) contain @ least 1 number" << endl;     cout << "3) contain @ least 1 upper case letter" << endl;     cout << "4) contain @ least 1 lower case letter" << endl;     cout << " " << endl;      cout << "please enter password now: " << endl;     cin >> password;  }  short passwordalphanumcheck(string password) {     short flag[3] = {0};      for(int = 0; password[i] != '\0'; i++)     {         if(isdigit(password[i]))             flag[0] = 1;         if(isupper(password[i]))             flag[1] = 2;         if(islower(password[i]))             flag[2] = 4;     }     return (flag[0] + flag[1] + flag[2]);  }  void passwordvalidationoutput(string password) {     short test = passwordalphanumcheck(password);      while ((password.length() < 8) && (test < 7))     {         while (password.length() < 8)         {             cout << " " << endl;             cout << "your password not correct size." << endl;             cout << "please re-enter password." << endl;             cin >> password;          }          cout << " " << endl;          test = passwordalphanumcheck(password);          cout << password         << " "         << (test == 0 ? "not supposed cause error. report inept computer engineer." :             test == 1 ? "not valid because missing upper , lower case letter." :             test == 2 ? "not valid because missing lower case letter , number." :             test == 3 ? "not valid because missing lower case letter." :             test == 4 ? "not valid because missing upper case letter , number" :             test == 5 ? "not valid because missing upper case letter." :             test == 6 ? "not valid because missing number." : "valid") << endl;          while (test < 7)         {             cout << " " << endl;             cout << "please re-enter password." << endl;             cin >> password;         }     }      cout << " " << endl; }  void passwordmatch(string password, string second_password) {     cout << "please enter password again, verification" << endl;     cin >> second_password;      while (!password.compare(second_password))     {         cout << "your passwords not match." << endl;         cout << " " << endl;         cout << "please re-enter password." << endl;         cin >> second_password;      }      cout << "your password has been approved!" << endl;  } 

your user input function passes password parameter value, not going change password variable outside function. when password gets passed validation function, still has default empty value.

if want input function modify password value, should pass reference.

void userinput(string& password); 

your validation function intended change value of password variable should take parameter reference.

void passwordvalidationoutput(string& password); 

your second_password variable has no purpose outside passwordmatch function, suggest change signature of function to:

bool passwordmatch(string password); 

inside function, can declare string second_password right before read it.

one last thing. in first loop inside passwordalphanumcheck, should change password[i] != '\0' i != password.length(). checking end of string comparing char against null character valid c-style strings , not necessary c++ standard library strings.