Password Verification Program "Assertion Error (unsigned)(c+1) <=256"

I am trying to build a program that takes a user password (6+ characters, One uppercase, one lower case, and one number), and checks for errors. The idea is that if the user is doing something wrong (say, forgetting to use an uppercase letter), the program will tell them what the error is, and prompt them to enter the program again.

I get through the building process without errors, but whenever I run the program, I get this error:

http://imgur.com/QF8SosE

I am new to C++, and am hoping somebody here with more experience can help me figure out why I am getting this error, because I have no clue! Here is my code:
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
82
83
84
85
86
87
88
#include <iostream>
#include <cctype>
using namespace std;

bool checkPass(char [], int); //Password Checking Function
char convert(string);


int main()
{
	//Holds C-String Size
	const int SIZE = 20;
    char password[SIZE]; //Holds Password
	bool check; //Variable for Password Check
	char *pass; //Points to password
	
	//Gets password from user
	cout << "Please create a password (must contain upper case letter, lowercase letter, ";
	cout << "and numumerican diget).\n" << endl;
	cout << "Password: ";
	cin.get(password, SIZE);
	pass = password;

	//Password runs through checkPass.  If checkPass finds anything
	//wrong, it tells the user what was wrong with the password, 
	//and allows them to enter a new one.
	check = checkPass (pass, SIZE);
	while (check = false){
		cout << "Password: ";
		cin.getline(password, SIZE);
		check = checkPass(password, SIZE);
	}
	
	//Dispaying password for user
	cout << "\nYour password is " << *pass << ".\n";
	return 0;
}

bool checkPass (char pass[], int size)
{
	int charCount = 0; //holds number of charcters for length check
	int count;

	//Gets number of characters, and checks for length
	for (count = 0; count <= size; count++)
	{
		if (isprint(pass[count]))
		{
			charCount++;
		}
	}
	if (charCount < 6) 
	{
		cout << "Please use six or more characters\n" << endl;
		return false;
	}

	//Checks for Uppercase Letter
	for (count = 0; count <= size; count++)
	{
		if (!isupper(pass[count]))
		{
			cout << "Please include at least one Uppercase letter\n" << endl;
			return false;
		}
	}

	//Checks for Lowercase Letter
	for (count = 0; count <= size; count++)
	{
		if (!islower(pass[count]))
		{
			cout << "Please include at least one Lowercase letter\n" << endl;
			return false;
		}
	}

	//Checks for Number
	for (count = 0; count <= size; count++)
	{
		if (!isdigit(pass[count]))
		{
			cout << "Please include at least one number\n" << endl;
			return false;
		}
	}
	return true;
}


According to the box, the error appears on line 56 and 68. Thanks!
Last edited on
count should be < size, not <= size. In fact, you should check only the elements of pass that have actually been written.

Also, your loops after line 58 don't check what you think. They check, in turn, that pass has only upper case characters, that pass has only lower case characters, and that pass has only numbers.
I changed it around but still seem to be getting the same error:
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <cctype>
using namespace std;

bool checkPass(char [], int); //Password Checking Function
char convert(string);


int main()
{
	//Holds C-String Size
	const int SIZE = 1000;
    char password[SIZE]; //Holds Password
	bool check; //Variable for Password Check
	char *pass; //Points to password
	
	//Gets password from user
	cout << "Please create a password (must contain upper case letter, lowercase letter, ";
	cout << "and numumerican diget).\n" << endl;
	cout << "Password: ";
	cin.get(password, SIZE);
	pass = password;

	//Password runs through checkPass.  If checkPass finds anything
	//wrong, it tells the user what was wrong with the password, 
	//and allows them to enter a new one.
	check = checkPass (password, SIZE);
	while (check = false){
		cout << "Password: ";
		cin.getline(password, SIZE);
		check = checkPass(password, SIZE);
	}
	
	//Dispaying password for user
	cout << "\nYour password is " << *pass << ".\n";
	return 0;
}

bool checkPass (char pass[], int size)
{
	int charCount = 0; //holds number of charcters for length check
	int upperCount = 0;
	int lowerCount = 0;
	int numCount = 0;
	int specCount = 0;
	int count;

	//Gets number of characters, and checks for length
	for (count = 0; count < size; count++)
	{
		if (isprint(pass[count]))
		{
			charCount++;
		}
	}
	if (charCount < 6) 
	{
		cout << "Please use six or more characters\n" << endl;
		return false;
	}

	//Checks for Uppercase Letter
	for (count = 0; count < charCount; count++)
	{
		if (isupper(pass[count]))
		{
			upperCount++;
		}
	}

	//Checks for Lowercase Letter
	for (count = 0; count < charCount; count++)
	{
		if (islower(pass[count]))
		{
			lowerCount++;
		}
	}

	//Checks for Number
	for (count = 0; count < charCount; count++)
	{
		if (isdigit(pass[count]))
		{
			numCount++;
		}
	}

	//Sends back error if any are abscent
	if(upperCount = 0)
	{
		cout << "INVALID: Please include at least 1 uppercase letter\n" << endl;
		return false;
	}
	else if (lowerCount = 0)
	{
		cout << "INVALID: Please include at least 1 lowercase letter\n" << endl;
		return false;
	}
	else if (numCount = 0)
	{
		cout << "INVALID: Please include at least 1 number\n" << endl;
		return false;
	}
	else
	{
	    return true;
	}
}
isprint() will also fail if you pass it characters that haven't been written. You don't even need it. The end of the string is marked by a zero. Not a '0', a 0; (char)0.
You need to break the loop when you find it. Even better, just use strlen() or strnlen().
Last edited on
> Assertion Error (unsigned)(c+1) <=256

This appears to be a really silly bug in the Microsoft offering.
(It happens only with 'Start Debugging' from the IDE when there is no breakpoint at the line in question.)

Do something like:
1
2
3
// isprint(pass[count])
unsigned char c = pass[count] ;
isprint(c) ;

and the assertion failure goes away.

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 <cctype>

bool checkPass( const char[], int ) ; // make it const-correct.

int main()
{
    const int SIZE = 20;
    char password[SIZE]; //Holds Password

    //Gets password from user
    std::cout << "Please create a password (must contain upper case letter, lowercase letter, ";
    std::cout << "and numumerican diget).\n" << '\n';

    //Password runs through checkPass.  If checkPass finds anything
    //wrong, it tells the user what was wrong with the password,
    //and allows them to enter a new one.
    bool check = false ;

    while( !check )
    {
        std::cout << "Password: ";
        std::cin.getline( password, SIZE );

        // std::cin.gcount() gives us the number of characters that were read by std::cin.get()
        int num_chars_read = std::cin.gcount() ;
        check = checkPass( password, num_chars_read );
    }

    //Dispaying password for user
    std::cout << "\nYour password is " << password << ".\n";
}

bool checkPass( const char pass[], int nchars )
{
    int n_printable = 0, n_uppercase = 0, n_lowercase = 0, n_digits = 0 ;

    for( int count = 0; count <= nchars ; ++count )
    {
        //***********************************************
        unsigned char c = pass[count] ; 
        //***********************************************

        if( std::isprint( c ) )
        {
            ++n_printable ;
            if( std::isupper( c ) ) ++n_uppercase ;
            else if( std::islower( c ) ) ++n_lowercase ;
            else if( std::isdigit( c ) ) ++n_digits ;
        }
    }

    if( n_printable < 6 )
    {
        std::cout << "Please use six or more printable characters\n" << '\n';
        return false;
    }

    //Checks for Uppercase Letter
    if( n_uppercase == 0 )
    {
        std::cout << "Please include at least one Uppercase letter\n" << '\n';
        return false;
    }

    //Checks for Lowercase Letter
    if( n_lowercase == 0 )
    {
        std::cout << "Please include at least one Lowercase letter\n" << '\n';
        return false;
    }

    //Checks for Number
    if( n_digits == 0 )
    {
        std::cout << "Please include at least one digit\n" << '\n';
        return false;
    }

    return true;
}

http://rextester.com/VFFTU37895
/* 13 */ std::cout << "and numumerican diget).\n" << '\n';
'diget' is spelled digit. ;)
Topic archived. No new replies allowed.