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   #16
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   #17
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   #18
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   #19
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   #20
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, 10:51 PM   #21
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-23-2010, 11:09 PM   #22
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:14 PM   #23
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, 11:21 PM   #24
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-24-2010, 12:41 AM   #25
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-24-2010, 09:05 AM   #26
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
When I call containDigit("abc")...

Quote:
Please enter a password: abcdef
Passwords must include at least on digit (1-9)
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...
If a condition is met, he loop "toggles" to the next false condition. If you DON'T meet the condition, you are stuck at that point until it is met (calling that same bool function with your input). Once you enter the correct input, the loop falls through to evaluate the next condition. Repeat. No matter what you input into my program, you can't make it break, and you can't make it behave wrongly. It works with 100% accuracy.

Again, the while function simply decides whether to enter the loop or not at each iteration.

Quote:
A function can only be false if it is called and executed.
That seems to make sense, but running my program suggests otherwise.

I find it hard to argue with the fact that what I've done works. Because it works.

Quote:
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.
I am interested to test this, but not right now, as I am about to leave to go to our cabin in the country where we have no internet and no cell phone service! Thanks for your thoughts and conversation on this--I am new to this and it really helps me to bounce ideas off of people such as yourself that I trust are logical. :::checking out:::
__________________
******************
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, 01:43 PM   #27
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
I find it hard to argue with the fact that what I've done works. Because it works.
Your solution is perfectly acceptable for a classroom assignment. But you might get your ass kicked in a code review for settling for "it works."

In the real world, you want functions to have as few side-effects as possible. Ideally, a function would have no side-effect other than its return value. Some languages even ENFORCE this restriction.

Your solution depends on side effects. This makes it harder to reason about how and why it works. Happy Monkey's does not depend on side effects, and is easier to grok.

(I hope this post doesn't come off as critical. I'm just trying to add a little more to think about.)
Perry Winkle is offline   Reply With Quote
Old 11-24-2010, 08:17 PM   #28
fo0hzy
Dirty cigarette-smoking lowlife
 
Join Date: Jul 2008
Location: Michigan
Posts: 85
Quote:
Originally Posted by Perry Winkle View Post
Your solution is perfectly acceptable for a classroom assignment. But you might get your ass kicked in a code review for settling for "it works."

In the real world, you want functions to have as few side-effects as possible. Ideally, a function would have no side-effect other than its return value. Some languages even ENFORCE this restriction.

Your solution depends on side effects. This makes it harder to reason about how and why it works. Happy Monkey's does not depend on side effects, and is easier to grok.

(I hope this post doesn't come off as critical. I'm just trying to add a little more to think about.)
Nerds.
fo0hzy is offline   Reply With Quote
Old 11-28-2010, 07:47 PM   #29
Flint
Snowflake
 
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
Quote:
Originally Posted by Perry Winkle View Post
Your solution depends on side effects.
This is, of course, semantics, but no, it doesn't.

I had a solution to design: how to test for two conditions.

I figured, while either one is false, correct that one.

Quote:
while !(a) or !(b)
{
if !(a)
call (a)

if !(b)
call (b)
}

What could be simpler, or easier to understand?

Let's say I am leaving the house. Did I lock the door (a)? Did I turn off the lights (b)? While either one is false: if (a) is false I lock the door, if (b) is false I turn off the lights. When neither one is false I am done. This is common sense.

I understand that if I were doing this in the "real world" things would be more involved and this might cease to be feasible for a variety of reasons that I have yet to consider, but in essence, this is my conception of how loops work and what they are supposed to (are DESIGNED to) do.

What I don't understand is why you guys find what I did confusing. I used regular, human logic.

Quote:
In the real world, you want functions to have as few side-effects as possible. Ideally, a function would have no side-effect other than its return value. Some languages even ENFORCE this restriction.
What are the undesirable "side effects" of my functions??? My functions didn't do anything but return a value until I moved some parts of main function into them so you guys could understand the elegant simplicity of my while/if/if loop.

I could put it back this way:

Code:
while ((!passLength(password)) || (!containDigit(password))) 
{
if (!passLength(password))
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password1: ";
cin.getline(password, SIZE);
(passLength(password)); //(a)
}

if (!containDigit(password))
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password2: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b)
} 
}
Does that help you to understand that I designed it this way on purpose? This is not accidental, there are no "side effects" ...
__________________
******************
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-28-2010 at 08:03 PM.
Flint is offline   Reply With Quote
Old 11-29-2010, 10:59 AM   #30
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
This is, of course, semantics, but no, it doesn't.

I had a solution to design: how to test for two conditions.

I figured, while either one is false, correct that one.


What could be simpler, or easier to understand?
Quote:
while !(a) or !(b)
{
if !(a)
call (a)

if !(b)
call (b)
}
You could remove both if statements altogether. With the side effects that a() and b() have, this code would be equivalent:

Quote:
while !(a) or !(b)
{
}
This is because !(a) is actually calling a(). If the test fails, then the password is changed (the side effect in question). This loop will run until the password makes it through both tests without being changed.

Quote:
What I don't understand is why you guys find what I did confusing. I used regular, human logic.
A big one is that you seem to think that the a's are different in:
Quote:
if (!a)
call a()
They are the same. Whatever is done in one will be done in the other.
Quote:
What are the undesirable "side effects" of my functions??? My functions didn't do anything but return a value until I moved some parts of main function into them so you guys could understand the elegant simplicity of my while/if/if loop.

I could put it back this way:

Code:
while ((!passLength(password)) || (!containDigit(password))) 
{
if (!passLength(password))
{
cout << "Passwords must be at least 6 characters long" << endl;
cout << "Please enter a password1: ";
cin.getline(password, SIZE);
(passLength(password)); //(a)
}
 
if (!containDigit(password))
{
cout << "Passwords must include at least on digit (1-9)" << endl;
cout << "Please enter a password2: ";
cin.getline(password, SIZE);
(containDigit(password)); //(b)
} 
}
Does that help you to understand that I designed it this way on purpose? This is not accidental, there are no "side effects" ...
(assuming there is no longer a getline in the tests, the lines I crossed out can be removed)
That is completely different. The tests no longer can change the password. That is logically a good program, though it could be more efficient.

Moving the parts of main() into the tests did not result in an equivalent program. You could not with this programn (as I did above with the other one) remove the entire contents of the while loop and have an equivalent program.
__________________
_________________
|...............| 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 03:30 AM.


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