Home > front end >  Input again and again until password becomes good
Input again and again until password becomes good

Time:11-14

Hi I want to set a condition that allows the user to input again and again until the password becomes good and then end the program. here is my code which only run one time, but I want to run it again and again until password becomes good

#include <iostream>
using namespace std;

int main()
{
  int l_case=0, u_case=0, digit=0, special=0;
  string str;
  cout<<"Enter Any Password"<<endl; 
  cin>>str;
  int l=str.length(),i; 
  for(i=0;i<l;i  )
  {
    if(islower(str[i]))
      l_case=1;
    if(isupper(str[i]))
      u_case=1;
    if(isdigit(str[i]))
      digit=1;
    if(!isalpha(str[i]) && !isdigit(str[i]))
      special=1;    
  }
  if(l_case && u_case && digit && special && l>=8)
    cout<<"Good Password"<<endl;
  else if((l_case u_case digit special>=3) && l>=6)
    cout<<"Bad Password"<<endl;
  else
    cout<<"Bad Password"<<endl;
  return 0;
}

CodePudding user response:

Here's the pseudocode of the logic you need to implement:

while (true)
    print "Enter the password"
    read str
    if password_is_good(str)
        break;
    else
        print "invalid password"

CodePudding user response:

Just add that code into a loop like this:

#include <iostream>
using namespace std;

int main() {
  int l_case = 0, u_case = 0, digit = 0, special = 0;
  string str;
  size_t l;
  do {
    cout << "Enter Any Password" << endl;
    cin >> str;
    l = str.length();
    size_t i;
    for (i = 0; i < l; i  ) {
      if (islower(str[i]))
        l_case = 1;
      if (isupper(str[i]))
        u_case = 1;
      if (isdigit(str[i]))
        digit = 1;
      if (!isalpha(str[i]) && !isdigit(str[i]))
        special = 1;
    }
  } while (!(l_case && u_case && digit && special && l >= 8));
  return 0;
}
  •  Tags:  
  • c c
  • Related