![]() |
|
Technology Computing, programming, science, electronics, telecommunications, etc. |
![]() |
|
Thread Tools | Display Modes |
![]() |
#16 | |||
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
Quote:
Quote:
In a nutshell, this is how I check for two conditions: Quote:
__________________
****************** 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. |
|||
![]() |
![]() |
![]() |
#17 |
Read? I only know how to write.
Join Date: Jan 2001
Posts: 11,933
|
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. |
![]() |
![]() |
![]() |
#18 | ||
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:
Here is the program running: Quote:
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 |
||
![]() |
![]() |
![]() |
#19 | ||
I think this line's mostly filler.
Join Date: Jan 2003
Location: DC
Posts: 13,575
|
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:
Every time you call if (!a()), you are calling a(). Quote:
__________________
_________________ |...............| We live in the nick of times. | Len 17, Wid 3 | |_______________| [pics] |
||
![]() |
![]() |
![]() |
#20 | |||
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
Quote:
Quote:
Quote:
__________________
****************** 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 |
|||
![]() |
![]() |
![]() |
#21 |
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
double 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.
__________________
****************** 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 |
![]() |
![]() |
![]() |
#22 | |
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:
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 |
|
![]() |
![]() |
![]() |
#23 | |
Esnohplad Semaj Ton
Join Date: Feb 2005
Location: A little south of sanity
Posts: 2,259
|
Quote:
![]() Either way you should at least store the values of a and b each time through the loop. |
|
![]() |
![]() |
![]() |
#24 |
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)); 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 |
![]() |
![]() |
![]() |
#25 | ||
I think this line's mostly filler.
Join Date: Jan 2003
Location: DC
Posts: 13,575
|
Quote:
Code:
int index = 0; int length = strlen(password); for (index = 0; index < length; index++ ) { if (isdigit(password[index])) return true; } Code:
cout << "Passwords must include at least on digit (1-9)" << endl; cout << "Please enter a password: "; cin.getline(password, SIZE); And then: Code:
return 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:
__________________
_________________ |...............| We live in the nick of times. | Len 17, Wid 3 | |_______________| [pics] |
||
![]() |
![]() |
![]() |
#26 | |||
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
When I call containDigit("abc")...
Quote:
Again, the while function simply decides whether to enter the loop or not at each iteration. Quote:
I find it hard to argue with the fact that what I've done works. Because it works. Quote:
__________________
****************** 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 |
|||
![]() |
![]() |
![]() |
#27 | |
Esnohplad Semaj Ton
Join Date: Feb 2005
Location: A little south of sanity
Posts: 2,259
|
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. 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.) |
|
![]() |
![]() |
![]() |
#28 | |
Dirty cigarette-smoking lowlife
Join Date: Jul 2008
Location: Michigan
Posts: 85
|
Quote:
|
|
![]() |
![]() |
![]() |
#29 | ||
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
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:
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:
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) } }
__________________
****************** 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. |
||
![]() |
![]() |
![]() |
#30 | ||||||
I think this line's mostly filler.
Join Date: Jan 2003
Location: DC
Posts: 13,575
|
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
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] |
||||||
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|