hello i'm trying to figure out how to write a program in c++ that check when i insert a password and force me to add at least 2 capital letters and 2 numbers in the password (you've seen this before right? when signing up for new account and checks for password stringth)
otherwise won't accept it and gives the output "password not accepted"
anyone can write a simple code for that?

Recommended Answers

All 7 Replies

No one is going to write the code for you. If write some code youself and you have an issue with it then post the code you have and what the issue is. also include any sample data and expected output. See http://sscce.org for more on writing a good code question.

i didn't mean to write a complete program, i already know the basics, but i'm stuck in the part that makes my letters in the program case-sensitive, which i just wanted to know what code i should use to make it so
that's all what i'm asking a simple line of code and from it i can move on

Well if you store the password as a std::string then you can iterate over each character and check if it is upper or lower case with std::isupper and std::islower. To count upper case letters it would look like:

int uppercaseCounter = 0;
std::string password = "PassWord";
for (auto& e : password)
{
    if (std::isupper(e))
        uppercaseCounter++;
}
std::cout << uppercaseCounter;

See this Live Example

commented: thanks +0

You can also try this.
This is according to your post

hello i'm trying to figure out how to write a program in c++ that check when i insert a password and force me to add at least 2 capital letters and 2 numbers in the password (you've seen this before right? when signing up for new account and checks for password stringth)

Below is a simple code to mimic such thing.

bool chkUperDegitPwd( const std::string pwd){
    static size_t sCount=0; // static count

   for(auto &p: pwd){  // >= c++11
        if(std::isdigit(p)){
            sCount++;
        }else if(std::isalpha(p)){
           if(std::isupper(p)){
               sCount++;
            }
        }
   }
   return pwd.length() == sCount ?true:false;
}

// use case
std::string pwd;
std::getline(std::cin,pwd);
if(chkUperDegitPwd(pwd)){
  //do your stuff
}else{
 // password error
}

Use std algorithms where possible:

#include <string>
#include <algorithm>

// Store the password in a string
std::string password;

/*
    Do things to get password from user
*/

// Count the upper and lower case letters
size_t upper_case_count = std::count_if( password.cbegin(), password.cend(), std::isupper );
size_t lower_case_count = std::count_if( password.cbegin(), password.cend(), std::islower );

@NathanOliver: You're correct, it doesn't compile. In this case the compilation failure is because std::isupper takes a locale as well as the character to test. Using the C function isupper does compile though (and does the right thing):

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
  std::string s( "Hello World!" );

  size_t count = std::count_if( s.cbegin(), s.cend(), isupper );

  std::cout << "Found " << count << " capitals" << std::endl;
}

Using GCC 4.9.2 (on Cpp.sh)

No need for a lambda in this case

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.