The Cellar  

Go Back   The Cellar > Main > Technology

Technology Computing, programming, science, electronics, telecommunications, etc.

Reply
 
Thread Tools Display Modes
Old 11-23-2010, 11:09 PM   #1
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
triple post

Happy Monkey's suggested code from post #15 also works.

Using the else condition to exit the loop, otherwise prompting for the password at the top of the loop. This is more concise.

Quote:
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: abc123
Thank you that is a valid password

Press Enter to close window...
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()
{
	bool ok=false;
	while ( ! ok )
	{
    cout << "Please enter a password: ";
    cin.getline(password, SIZE);

    if (! passLength(password))
		{
		cout << "Passwords must be at least 6 characters long" << endl;
		} 
    else if ( ! containDigit(password) )
		{
		cout << "Passwords must include at least on digit (1-9)" << endl;
		}
    else
		{
		ok=true;
		}
  }


	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 
			{
			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;
	}
		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
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 06:52 AM.


Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.