![]() |
|
Technology Computing, programming, science, electronics, telecommunications, etc. |
![]() |
|
Thread Tools | Display Modes |
|
![]() |
#1 |
I think this line's mostly filler.
Join Date: Jan 2003
Location: DC
Posts: 13,575
|
I think I was wrong about the bad behavior again; it should work. But it is very complicated. The test functions can return false when the password has been changed to a good one. The structure of the while loop corrects for it, but the behavior of the functions is not what one would expect.
The snippet I posted would give the same behavior, but is much simpler. This would allow the error reporting to be in the loop, and only calls each test once: Code:
bool passLength(string) // just return true/false. Don't read a new pw or print error bool containDigit(string) // just return true/false. Don't read a new pw or print error string getPW() { string password; 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; } } return password; }
__________________
_________________ |...............| We live in the nick of times. | Len 17, Wid 3 | |_______________| [pics] |
![]() |
![]() |
![]() |
#2 | |||
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. |
|||
![]() |
![]() |
![]() |
#3 |
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. |
![]() |
![]() |
![]() |
#4 | ||
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 |
||
![]() |
![]() |
![]() |
#5 | ||
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] |
||
![]() |
![]() |
![]() |
#6 | |||
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 |
|||
![]() |
![]() |
![]() |
#7 | |
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. |
|
![]() |
![]() |
![]() |
#8 |
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 |
![]() |
![]() |
![]() |
#9 | ||
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] |
||
![]() |
![]() |
![]() |
#10 | |||
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 |
|||
![]() |
![]() |
![]() |
#11 | |
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.) |
|
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|