View Single Post
Old 11-23-2010, 09:20 PM   #4
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
@tw: (a) and (b) are bool functions. while !(a) does not cause if !(a) to drop. while !(a) OR !(b) simply makes the decision to enter the loop or not.

Quote:
Originally Posted by tw
Everytime (a) and (b) are executed - an new entry occurs.
Correct. That is what makes this work. The loop continuously evaluates, "falling through" true conditions and "catching" on false conditions. false conditions always prompt for a new password and immediately reevaluate the same condition. The loop continues to evaluate until it is able to "fall through" completely (exit the loop).

Here is the program running:

Quote:
Please enter a password: a
Passwords must be at least 6 characters long
Please enter a password: abc
Passwords must be at least 6 characters long
Please enter a password: abcdef
Passwords must include at least on digit (1-9)
Please enter a password: 123
Passwords must be at least 6 characters long
Please enter a password: abcdef
Passwords must include at least on digit (1-9)
Please enter a password: a1
Passwords must be at least 6 characters long
Please enter a password: abc123
Thank you that is a valid password

Press Enter to close window...
Here is the code:

Code:
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
	
	//Function checks the password for length.		(a)
	bool passLength(char[]);

	//Function checks the password for a digit.		(b)
	bool containDigit(char[]);
	
	const int SIZE = 21;
	char password[SIZE];

int main()
{
	cout << "Please enter a password: ";
	cin.getline(password, SIZE);
	
	while ((!passLength(password)) || (!containDigit(password)))  
		{
	if	(!passLength(password))
		(passLength(password));						//(a)

	if	(!containDigit(password))
		(containDigit(password));					//(b)	
		}

	cout << "Thank you that is a valid password" << endl;
	
	
	//Keep the window open until Enter key is pressed.
	cout << "\nPress Enter to close window..." << endl;
	std::cin.get();

	return 0;
}	

	bool passLength(char password[])				//(a)
{
	int lengthPass = 6;
	int length = strlen(password);
	
		if (lengthPass <= length)
			return true;
		else 
			{
			cout << "Passwords must be at least 6 characters long" << endl;
			cout << "Please enter a password: ";
			cin.getline(password, SIZE);
			return false;
			}
}


	bool containDigit(char password[])				//(b)
{
	int index = 0;
	int length = strlen(password);
	
	for (index = 0; index < length; index++ )
	{
		if (isdigit(password[index]))
			return true;
	}
		cout << "Passwords must include at least on digit (1-9)" << endl;
		cout << "Please enter a password: ";
		cin.getline(password, SIZE);		
		return false;
	
}
__________________
******************
There's a level of facility that everyone needs to accomplish, and from there
it's a matter of deciding for yourself how important ultra-facility is to your
expression. ... I found, like Joseph Campbell said, if you just follow whatever
gives you a little joy or excitement or awe, then you're on the right track.

. . . . . . . . . . . . . . . . . . . . . . . . . . Terry Bozzio
Flint is offline   Reply With Quote