isdigit function

I'm creating a password verification program (using c-strings) with one of the criteria to have a number within it. My function is causing a fatal assertion error.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool digit(char str[], const int LEN)
{	int count = 0;
	for(int i = 0; i < LEN; i++)
	{	if(isdigit(str[i]))
			count++;
	}
	if(count > 0)
		return true;
	else
	{	cout << "Password must have at least one number.\n";
		return false;
	}
}


Thanks ahead.
Why is str not constant, and what do you call the function with?
const int LENGTH = 21;
char input[LENGTH];
digit(input, LENGTH)
Last edited on
Why do you pass an uninitialized array? Either way, you should pass LENGTH-1 -- you don't want to check the zero character at the end, do you?

Also... the error output really shouldn't be inside the digit function. You return a bool, so the client code can tell whether the check was successful, and decide how to handle that case.

Anyways, you are using VC++ right? Maybe you should just run the debugger and see what exact line causes the error.
The array isn't uninitialized. Here is my code from main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int LENGTH = 21;
    char input[LENGTH];
	bool flag = false;
    cout << "Please enter a password: ";
    cin.getline(input, 20);
    do
	{	if(length(input) && capital(input) && digit(input, LENGTH) && symbol(input) && space(input))
			flag = true;
		else
		{	cout << "Please enter a password: ";
			cin.getline(input, 20);
		}
	}while(flag == false);
	
	cout << "You have entered a successful password. Thank you, goodbye.\n";

    return 0;


As you can see, if I put all my bool functions in an if statement, it doesn't leave room for error reporting unless I call the function again. I wanted to have criteria specific prompts for why the password isn't sufficient since I don't list them all out at the beginning.

I don't have any debugger code. The program compiles fine and then crashes.
Last edited on
Have you tried what Hanst99 said?
Why do you pass an uninitialized array? Either way, you should pass LENGTH-1 -- you don't want to check the zero character at the end, do you?


In case for any reason you're running off the end of the array for input.


Edit:
Ok, I just more or less copied your code and solved it. I think you were running off the end of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int _tmain(int argc, _TCHAR* argv[])
{
	const int LENGTH = 21;

	char input[ LENGTH ];

	std::cout << "Enter text: ";
	std::cin.getline( input, LENGTH );

	int count = 0;

	int tempCount = strlen( input );

	for( int i = 0; i < tempCount; i++ )
	{
		if( isdigit( input[ i ] ) )
			count++;
	}

	std::cout << "There were " << count << " digits.\n\n";

	return 0;
}


The above code fixed my assertion failure. The use of strlen( input ) to get the actual size of the text entered rather than the size of the array and itterating through that ammount to check it all.
Last edited on
I don't have any debugger code.

Yes you have, VC++ automatically generates it in the debug build.
Yes, thank you linx. I was entering a password of six characters and my for loop was testing characters 1 to 21.

I replaced i < LEN with str[i]!='\0'
Topic archived. No new replies allowed.