Use of cctype library(new to programming)

I am trying to write a program to check the
security level of a user input password. The program is supposed to report the security level based on the following 5-star scheme:
 contains at least one lower case letter
 contains at least one upper case letter
 contains at least one digits
 has a length longer than 6
 contains at least one symbol (non-letter and non-digit)
 1 star for each items (hence, maximum 5 stars)

But unfortunately it does not work and i do not know why, can anyone please help?

Also, Read the user input as characters until the user input ‘!’, but i don't know how can i accomplish this, plz help.

Thank You

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
  #include <cctype>
using namespace std;


int size ;
int main(){
char *pw ;
pw = new char[size];
int lvofsec = 0;

cin >> pw[size];

for(int i = 0; i <= (sizeof(pw) - 1); i ++){

    if(islower(pw[i])){
        lvofsec = lvofsec + 1;
        break;
    }
}

for(int j = 0; j <= (sizeof(pw) - 1); j ++){

    if(isupper(pw[j])){
        lvofsec = lvofsec + 1;
        break;
    }
}

for(int k = 0; k <= (sizeof(pw) - 1); k ++){

    if(isdigit(pw[k])){
        lvofsec = lvofsec + 1;
        break;
    }
}

for(int m = 0; m <= (sizeof(pw) - 1); m ++){

    if(ispunct(pw[m])){
        lvofsec = lvofsec + 1;
        break;
    }
}

if(sizeof(pw) > 6){
    lvofsec = lvofsec + 1;
}

cout << "The security level of your password is " << lvofsec << "*" << endl;

return 0;
}
What exactly does not work?

Main mistake you made: sizeof(pw) will return either 4 of 8 depending on your processor architecture.
pw = new char[size]; size is initializated, so you are creating an array of random size (which have greate chance to be 0)
cin >> pw[size]; you are reading single character in location out of array bounds.
Once you've fixed that issue, you have a logical error. You are giving the password a "star" for every lower case character it has, not just for a single one. Similar problem for your other checks.
MiiNiPaa wrote:
size is initializated, so you are creating an array of random size (which have greate chance to be 0)

In this case, size is guaranteed to be initialized to 0, since it is defined in the global namespace.

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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool has_one_lower_case_letter(const std::string & s)
{
    for (std::size_t i = 0; i < s.size(); ++i)
        if (std::islower(s[i]))
            return true;

    return false;
}

bool has_one_upper_case_letter(const std::string& s)
{
    for (std::size_t i = 0; i < s.size(); ++i)
        if (std::isupper(s[i]))
            return true;

    return false;
}


int size;
int main(){

    std::string pw;
    std::cin >> pw;

    unsigned score = 0;
    if (has_one_lower_case_letter(pw))
        ++score;
    if (has_one_upper_case_letter(pw))
        ++score;

    // etc.
}
cire wrote:
it is defined in the global namespace
Oops, totally missed that

Read the user input as characters until the user input ‘!’
In cire code use following input method: std::getline(std::cin, pw, '!');. I must note that in this case ispunct() is not enough to denote "all other symbols" — it will skip whitespaces

EDIT: http://www.cplusplus.com/forum/beginner/124742/
Looks like usage of std::string is forbidden for this exercise
Last edited on
HKU student ha?
MiiNiPaa: i don't really understand about the size thing of the array, i am very new, is there any simpler explanation?

and how can i do so if std::string is not allowed?

marcuszheng: Yes

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
#include <cctype>
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
	char pw[100];
	cout << "Enter your password\n(No more than 100 characters): ";
	cin.getline(pw, sizeof pw);
	
	const int len = cin.gcount(); // get number of characters read
	int strength = 0;

	for(int i = 0; i < len; i++){
    	if( islower(pw[i]) ){
    		// ??? //
    	}
	}

	for(int i = 0; i < len; i++){
    	if(isupper(pw[i])){
    		// ??? //
    	}
	}

	for(int i = 0; i < len; i++){
    	if(isdigit(pw[i])){
    		// ??? //
    	}
	}

	for(int i = 0; i < len; i++){
    	if(!isalnum(pw[i]) && !isspace(pw[i])){
    		// ??? //
    	}
	}

	if(len > 6){
    	// ??? //
   	}

	cout << "The security level of your password is " <<
		setfill('*') << setw(strength + 1) << '\n';

	return 0;
}
Topic archived. No new replies allowed.