password verification

Hello, I am struggling with Password verification. Can someone please help me.

Problem with this code: Just sets the characters between 6 and 15.
and it does not verifies uppercase, lowercase, numeric and alphabet which it must contain.


#include <iostream>
#include <cstring>

using namespace std;
bool testPassword(char *);
int verifyUpper(char *);
int verifyLower(char *);
int verifyNum(char *);
int verifyAlpha(char *);

int main()
{
char password[100];

cout << "Enter the password";

cin.getline(password, 100);



while(strlen(password) < 6 || strlen(password) > 15)
{

cout << "PLease enter the password more than 10 but less than 15!!";

cin >> password;

}
while(strlen(password) >= 6 || strlen(password) <= 15)
{
verifyUpper(password);

}
while(strlen(password) >= 6 || strlen(password) <= 15)
{
testPassword(password);
}

}

bool testPassword(char *str)
{

int length = strlen(str);
return verifyUpper(str);
return verifyLower(str);
return verifyNum(str);
return verifyAlpha(str);

}
//upper function
int verifyUpper(char *str)
{
int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isupper(str[count]))
num++;
}
return num;
}

//lower function
int verifyLower(char *str)
{
int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!islower(str[count]))
num++;
}
return num;
}

//verify number contained
int verifyNum(char *str)
{ int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isdigit(str[count]))
num++;
}
return num;
}

int verifyAlpha(char *str)
{ int num = 0;
int length = strlen(str);
for (int count = 0; count < length; count++)
{
if (!isalpha(str[count]))
num++;
}
return num;
}



Consider using std::string https://cal-linux.com/tutorials/strings.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <cctype>

bool has_upper_case_char( const std::string& str )
{
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char c : str ) // for each character in the string
        if( std::isupper(c) ) // if it is an upper case character
            return true ; // str has an upper case character

    return false ; // none of the characters in str are upper case characters
}

bool has_lower_case_char( const std::string& str )
{
    for( char c : str ) if( std::islower(c) ) return true ;
    return false ;
}

bool has_alpha_char( const std::string& str )
{
    for( char c : str ) if( std::isalpha(c) ) return true ;
    return false ;
}

bool has_digit_char( const std::string& str )
{
    for( char c : str ) if( std::isdigit(c) ) return true ;
    return false ;
}

bool noisy_verify_password( const std::string& str )
{
    if( str.size() < 6 || str.size() > 16 ) // ideally use named constants for 6 and 16
    {
        std::cout << "incoorect length: must be between 6 and 16 characters\n" ;
        return false ;
    }
    else std::cout << "ok: password length between is between 6 and 16\n" ;

    if( !has_alpha_char(str) )
    {
        std::cout << "error: must contain an alpha character\n" ;
        return false ;
    }
    else std::cout << "ok: contains an alpha character\n" ;

    if( !has_upper_case_char(str) )
    {
        std::cout << "error: must contain an upper case character\n" ;
        return false ;
    }
    else std::cout << "ok: contains an upper case character\n" ;

    if( !has_lower_case_char(str) )
    {
        std::cout << "error: must contain a lower case character\n" ;
        return false ;
    }
    else std::cout << "ok: contains a lower case character\n" ;

    if( !has_digit_char(str) )
    {
        std::cout << "error: must contain a digit character\n" ;
        return false ;
    }
    else std::cout << "ok: contains a digit character\n" ;

    return true ;
}

int main()
{
    std::string candidate ;
    std::cout << "password? " ;
    std::getline( std::cin, candidate ) ;

    if( noisy_verify_password(candidate) ) std::cout << "good password!\n" ;
    else std::cout << "bad password!\n" ;
}

Note: this can be written with fewer lines of code and be made more efficient; but don't worry about that right now.
Thanks @JLBorges. Although I am not quite sure if my professor allows me to use 'cctype' because she has not gone through that type yet.
It's quite hard for me too, but i understood what you did there. Thank you again.
Last edited on
Topic archived. No new replies allowed.