-3

I'm just starting C++ and creating a simple password validation program. [edit] My goal is to display each messages if there is a lack of uppercase, lower case, digits, and/or special characters.

If there is only special character the output will be like this:

Enter your password: !                                                                                                                                                                  
Password must contain a lower case!                                                                                                                                                     
Password must contain an upper case!                                                                                                                                                    
Password must contain a digit!
Enter your password: 

what i want if all the requirements are met:

Enter your password: 1qQ+                                                                                                                                                                  
your password is created
program is finished                                                                                                                                                     

But what i got is infinite loop of "your password is created". Any solution or alternative to make my code better/efficient?

Sorry for bad intendation.

#include <iostream>
#include <string>

using namespace std;

void passcheck(string& password)
{
    bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecialchar = false;


    for (int i = 0; i < password.length(); ++i)
    {
        if (islower(password[i]))
        {
            hasLower = true;
        }
        else if (isupper(password[i]))
        {
            hasUpper = true;
        }
        else if (isdigit(password[i]))
        {
            hasDigit = true;
        }
        else if (password.find(" !#$%&'()*+,-.:;<=>?@[]^_`{|}~"))
        {
            hasSpecialchar = true;      
        }

    }

     do
   {
    if (!hasLower)
   {
       cout << "Password must contain a lower case!"<< endl;
   }
    if (!hasUpper)
   {
       cout << "Password must contain an upper case!"<< endl;
   }
    if (!hasDigit)
   {
       cout << "Password must contain a digit!"<< endl;
   }
    if(!hasSpecialchar)
   {
       cout << "Password must contain special char!"<< endl;
   }
   else
   {
     cout << "your password is created" << endl;
   }


   }while(hasSpecialchar && hasDigit && hasLower && hasUpper);

}
   


int main(int argc, char const *argv[])
{
    
    string password; 

    do{
    cout << "Enter your password: ";
    getline(cin, password);
    passcheck(password);
     }while(true);

     cout << "program is finished" << endl;

    cin.get();
    return 0;
}

  • 1
    Why is it a loop? Nothing changes inside it. – molbdnilo Aug 27 '22 at 10:05
  • Shouldn't you check for negation of these booleans in `while` statement? Ie as for now you're looping as long as password HAS special character, HAS digit etc. I guess that wasn't your intention. – PookyFan Aug 27 '22 at 10:06
  • 2
    Just negate the condition: `while (!(hasSpecialchar && hasDigit && hasLower && hasUpper))` or alternatively `while (!hasSpecialchar || !hasDigit || !hasLower || !hasUpper))` and the loop will end once all criterias are met. This of course assuming you do modify the values inside the loop body in your real code. Who taught you to use this kind of intendation btw? It's bad. – fabian Aug 27 '22 at 10:07
  • It would be useful if you had provided a minimal reproducible example. Your current code doesn't say much about the logic of your password validation algorithm. – digito_evo Aug 27 '22 at 10:35

2 Answers2

0

You want to execute the loop until all the boolean expressions are true I assume. This would evaluate to

do{
//your stuff
} while(!(hasSpecialchar && hasDigit && hasLower && hasUpper))`

But depending on what exactly you're trying to do there's probably a better approach then a do-while loop. Where do you set these booleans? If you're using a single thread, then this values aren't gonna change inside the loop and the whole loop doesn't make any sense.

po.pe
  • 1,047
  • 1
  • 12
  • 27
-1

you can use break keyword, like this

while (1) {
    ...
    if (cond) {
        break
    }
}
pulk66
  • 1