Hello Daniweb,

I am a student in my CSC 194 using Visual C++ 2008. I am designing a program according to these standards. Most of my knowledge is strictly textbook information so bare with me.

Imagine you are developing a software package that requires users to enter their own passwords. Your software requires that users’ passwords meet the following criteria:
• The password should be at least six characters long.
• The password should contain at least one uppercase and at least one lowercase letter.
• The password should have at least one digit.
Write a program that asks for a password and then verifies that it meets the stated criteria. If it doesn’t, the program should display a message telling the user why.

Now the code below does follow all these standards and runs accordingly. However, there is one error during running that prevents this code from being acceptable. If the user were to enter a password that is less than six characters, the dreaded debug assertion error occurs.

Line: 56
Expression: (unsigned)(c+1) <= 256

From researching the web, I know this has something to do with isdigit or the strlen functions, but the codes I found were too advanced for me to decipher and apply to my problem. Interestingly, this error does not occur if I check to see if the user has entered a password greater than 6 characters instead of less than 6. The program also runs as normal after ignoring the error window a few times. No one else in class appears to have encountered this error and I'm at a loss at what to alter in this code. I was going to approach my teacher but a classmate of mine recommended this website. So I wanted to see if you guys could be of help, first.

Thank you for your assistance and I hope I've given you enough information.

-DF

#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
bool testPassword (char pass[], const int SIZE);

int main()
{
	const int SIZE = 7;
	char pass[100], password[SIZE];
	int count = 0;

	cout << "Enter a password with at least " << (SIZE - 1) << " characters." << endl;
	cout << "Your password must contain a numerical digit, a lowercase letter," << endl; 
	cout << "and an uppercase letter." << endl;
	cin.getline(pass, 100);
	if (testPassword(pass, SIZE))
	{
		cout << "The password you entered: ";
		strncpy_s(password, pass, SIZE);
		while (password[count] != '\0')
		{
			cout << password[count];
			count++;
		}
		cout << " is valid!" << endl;
	}
	return 0;
}

bool testPassword (char pass[], const int SIZE)
{
	bool passUpper = false;
	bool passLower = false;
	bool passDigit = false;
	int length = strlen(pass);

	for (int i = 0; i < SIZE; i++)
	{
		if(isdigit(pass[i]))
			passDigit = true;
		if(islower(pass[i]))
			passLower = true;
		if(isupper(pass[i]))
			passUpper = true;
	}
	if(length < (SIZE-1))
		cout << "Error: Your password does not meet the requirement of " << (SIZE - 1) << " or more characters." << endl;	
	if (passDigit == false)
		cout << "Error: Your password does not contain a numerical digit." << endl;
	if (passLower == false)
		cout << "Error: Your password does not contain a lowercase letter." << endl;
	if (passUpper == false)
		cout << "Error: Your password does not contain an uppercase letter." << endl;

	if (length < (SIZE-1) || passDigit == false || passLower == false || passUpper == false)
	{
		cout << "Please restart the program and adhere to the given directions." << endl;
		return false;
	}
	else
		return true;
}

Recommended Answers

All 2 Replies

You're checking every character in the array, not every character in the string. The latter is likely to be much shorter, which means you'll be looking at a lot of indeterminate characters.

However, since you're already taking the length of the string, just change this:

for (int i = 0; i < SIZE; i++)

To this:

for (int i = 0; i < length; i++)

Ah so that's where I goofed. Yes it all makes sense and the program is running accordingly.

Thank you.

-DF

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.