title: Validating a password with c++ and giving specific errors
body: <p>I am trying to write a c++ program that validates a password that requires one uppercase letter, one lowercase letter, and a digit using functions. </p>
  
  <p>The issue is that I'm trying to display the specific errors that are occurring, not just "Invalid, try again.", but I'm having a hard time figuring out how to do that. It should keep asking until they enter a valid password.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;cctype&gt;
  
  using namespace std;
  
  int validate(string);
  string find(int);
  
  int main()
  {
      string pw;
      int val;
      string result;
  
  
      do{
              cout &lt;&lt; "Enter password: " &lt;&lt; endl;
              cin &gt;&gt; pw;
              val = validate(pw);
              cout &lt;&lt; find(val) &lt;&lt; endl;
  
        } while (val != 0);
  
  
  }
  //VALIDATES PASSWORD
  int validate(string pw)
  {
      int valid = 0;
      char c;
      int length = pw.length();
      bool digit = false;
      bool upper = false;
      bool lower = false;
  
      int i = 0;
  
      if (pw.length() &lt; 6)
          valid = 1;
  
      while (i &lt; pw.length())
      {
          c = pw[i];
          i++;
  
          if (isdigit(c))
          {
              digit = true;
              valid++;
          }
          if (isupper(c))
          {
              upper = true;
              valid++;
          }
          if (islower(c))
          {
              lower = true;
              valid++;
          }
  
          //Valid input
          if (length &gt;= 6 &amp;&amp; upper &amp;&amp; lower &amp;&amp; digit)
              valid = 0;
  
  }
  
  return valid;
  
  
  }
  //RETURNS STRING WITH PROBLEM
  string find(int valid)
  {
  
      string result;
  
      if (valid == 0)
      {
          result = "Valid Password ";
      }
      else
      {
          result = "Invalid Password: ";
  
          if (valid == 1)
              result = result + " Too short ";
          else if (valid == 2)
              result = result + " too short, needs a digit, and an uppercase letter";
          else if (valid == 3)
              result = result + " too short, needs a digit, and a lowercase letter";
          else if (valid == 4)
              result = result + " too short, and needs an uppercase letter";
          else if (valid == 5)
              result = result + " too short, and needs a lowercase letter";
          else if (valid == 6)
              result = result + " too short, needs a digit";
  
  
          else if (valid == 7)
              result = result + " Needs a didgit ";
          else if (valid == 8)
              result = result + " Needs digit and uppercase letter ";
          else if (valid == 9)
              result = result + " Needs digit and lowercase letter";
  
  
          else if (valid == 10)
              result = result + " Needs an uppercase letter ";
          else if (valid == 11)
              result = result + " Needs uppercase and lowercase letter";
  
          else if (valid == 12)
              result = result + " Needs a lowercase letter";
  
      }
  
      return result;
  }
  </code></pre>