The Cellar  

Go Back   The Cellar > Main > Technology
FAQ Community Calendar Today's Posts Search

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

Reply
 
Thread Tools Display Modes
Old 11-23-2010, 02:50 PM   #1
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
Quote:
Originally Posted by HM
The test functions can return false when the password has been changed to a good one.
If it is a good password the function cannot return false.

Quote:
Originally Posted by HM
The structure of the while loop corrects for it, but the behavior of the functions is not what one would expect.
No, man. The if condition corrects for it by not calling the function unless it is false. The while loop doesn't correct the behavior, the while loop just exits the loop before the next iteration.



In a nutshell, this is how I check for two conditions:
Quote:
while !(a) or !(b)
{
if !(a)
call (a)

if !(b)
call (b)
}
This will repeat until the conditions are met. It will also exit immediately when the conditions ARE met.
__________________
******************
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

Last edited by Flint; 11-23-2010 at 03:00 PM.
Flint is offline   Reply With Quote
Old 11-23-2010, 06:35 PM   #2
tw
Read? I only know how to write.
 
Join Date: Jan 2001
Posts: 11,933
Quote:
Originally Posted by Flint View Post
while !(a) or !(b)
{
if !(a)
call (a)

if !(b)
call (b)
}
Only reading the summary code: Are (a) and (b) a function or method? Or are each a property?

If (a) or (b) are functions: when a bad user name is entered in
while ( !(a) ....
That causes the while loop to drop to an 'If' condition. Then another
(a) function is executed.

Then the while loop again again executes (a) again:
while ( !(a) or !(b) )
Everytime (a) and (b) are executed - an new entry occurs. IOW what happened in "call (a)" gets replaced by another execution of "while ( !(a) ..."


To work, the code should read something like this:


do
{call (a)
if ((a).result == good)
{call (b)}
}
loop until ( (a).result == good and (b).result == good )


(a).result and (b).result are properties.
call(a) and call(b) are methods.

Summary code many not accurately reflect what your actual code does. But the difference between an executed function call(a) and its resulting property (a).result should be clearer. As I read the original summary code, everytime (a) is executed, a previous user name (good or bad) is erased and a new username is entered.
tw is offline   Reply With Quote
Old 11-23-2010, 09:20 PM   #3
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
Old 11-23-2010, 09:28 PM   #4
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
Quote:
Originally Posted by Flint View Post
If it is a good password the function cannot return false.
If the password going in is bad, abd the password entered in the function is good, then the function returns false, even though the password is good.
Quote:
No, man. The if condition corrects for it by not calling the function unless it is false.
It calls it no matter what. If that first call returns false, it calls the function a second time (third if you count the loop test).

Every time you call if (!a()), you are calling a().

Quote:
The while loop doesn't correct the behavior, the while loop just exits the loop before the next iteration.
While giving the user two more chances to change the password.
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote
Old 11-23-2010, 09:54 PM   #5
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
Quote:
Originally Posted by HM
Every time you call if (!a()), you are calling a().
Perhaps, but this is not the behavior I see while using Visual C++ 2010 Express.

Quote:
Please enter a password: abc
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...
Another example:

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...
__________________
******************
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
Old 11-23-2010, 11:14 PM   #6
Perry Winkle
Esnohplad Semaj Ton
 
Join Date: Feb 2005
Location: A little south of sanity
Posts: 2,259
Quote:
Originally Posted by Flint View Post
Perhaps, but this is not the behavior I see while using Visual C++ 2010 Express.
Maybe you're getting confused output because you're not flushing the output stream (i.e., flush(cout). Another, but less likely, answer might be that the function call is getting optimized away.

Either way you should at least store the values of a and b each time through the loop.
Perry Winkle is offline   Reply With Quote
Old 11-23-2010, 10:51 PM   #7
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
double post

Quote:
Originally Posted by Happy Monkey View Post
If the password going in is bad, abd the password entered in the function is good, then the function returns false, even though the password is good.
I'm interested to know why you think this is; as I cannot conceive of a logic where this makes sense. Aside from the fact that this isn't what happens. I'm just interested to hear your reasoning.
__________________
******************
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
Old 11-24-2010, 12:41 AM   #8
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
Quote:
Originally Posted by Flint View Post
I'm interested to know why you think this is; as I cannot conceive of a logic where this makes sense. Aside from the fact that this isn't what happens. I'm just interested to hear your reasoning.
Say you call containDigit("abc")
Code:
    int index = 0;
    int length = strlen(password);
    
    for (index = 0; index < length; index++ )
    {
        if (isdigit(password[index]))
            return true;
    }
It gets through these steps, and gets here:
Code:
        cout << "Passwords must include at least on digit (1-9)" << endl;
        cout << "Please enter a password: ";
        cin.getline(password, SIZE);
The user enters "abc1".
And then:
Code:
        return false;
The password is now "abc1", and containDigit() returned false.

You don't see the problem in your execution because your while loop corrects for the behavior of the test functions. But if you tested the functions individually, you would see it.

Quote:
Originally Posted by Flint View Post
This code only "calls" passLength IF passLength is false.

What exactly do you mean by "IF passLength is false"? A function can only be false if it is called and executed.

Unlike the solutions graciously suggested by Pete Zicato and Happy Monkey, in my code I do not require a superfluous bool variable. I use the bool function to evaluate itself. Maybe this is "wrong" but it works.
My first suggestion also doesn't need the bool, without the side effects in the test functions.
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote
Old 11-23-2010, 11:09 PM   #9
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
Old 11-23-2010, 11:21 PM   #10
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
The question is whether evaluating a bool function (for true or false) is the same as "calling" the function and executing the internal code of the function which is conditional on it being true or false. And the answer I am getting is that this is not the same, because my program runs and works.

This code only "calls" passLength IF passLength is false.
Code:
if	(!passLength(password))
	(passLength(password));
If it is false, this gives it another chance to be true. Repeat.

Unlike the solutions graciously suggested by Pete Zicato and Happy Monkey, in my code I do not require a superfluous bool variable. I use the bool function to evaluate itself. Maybe this is "wrong" but it works.
__________________
******************
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
Old 11-29-2010, 01:56 PM   #11
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
You are wrong. The 'true' or 'false' that a() evaluates to is the one that is returned in the body of the function as it executes. I don't know where you think that value comes from if it doesn't come from the execution of the function.

Try this:

Add a second parameter to a() and b(), and pass in a unique value to each call. At the beginning of a() and b(), print out that value.

Quote:
bool a(pw, x)
{
cout << "a " << pw << " " << x << endl;
...
}

bool b(pw, x)
{
cout << "b " << pw << " " << x << endl;
...
}

while !(a(pw,1)) or !(b(pw,2))
{
if !(a(pw,3))
call (a(pw,4))

if !(b(pw,5))
call (b(pw,6))
}
If you keep entering short, numberless passwords then you will see all six.
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote
Old 11-29-2010, 02:57 PM   #12
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
Everything that you've said was supposed to happen (posts #8, 13, 15, 19, 25) doesn't happen. I don't see how showing you another example that doesn't happen will be more convincing than the overwhelming evidence I've already presented.
__________________
******************
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
Old 11-29-2010, 05:51 PM   #13
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
It will happen.

I withdrew my predictions from 8 and 13 already.

I don't think you've tested whether the things I've said were supposed to happen in 15, 19, and 25 are happening. They wouldn't affect what you see in your normal execution, just what is happening behind the scenes. As I said, the while loop is correcting for the strange behavior of the functions, and it seems to me that all of the overwhelming evidence you have presented includes the while loop.

If you test the functions alone, not in a while loop, you can see the side effects:

main()
{
cin.getline(pw)
if (a(pw))
cout << "a returned true, pw is now " << pw << endl;
else
cout << "a returned false, pw is now " << pw << endl;
}

If you enter a bad password, it will ask for another. If you enter another one, it will return false, even if the second one was good.
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote
Reply


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

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 04:46 AM.


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