![]() |
|
Technology Computing, programming, science, electronics, telecommunications, etc. |
![]() |
|
Thread Tools | Display Modes |
|
![]() |
#1 | |||
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. |
|||
![]() |
![]() |
![]() |
#2 |
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. |
![]() |
![]() |
![]() |
#3 | ||
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 |
||
![]() |
![]() |
![]() |
#4 | ||
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] |
||
![]() |
![]() |
![]() |
#5 | |||
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 |
|||
![]() |
![]() |
![]() |
#6 | |
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. |
|
![]() |
![]() |
![]() |
#7 |
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 |
![]() |
![]() |
![]() |
#8 | ||
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] |
||
![]() |
![]() |
![]() |
#9 | |
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 |
|
![]() |
![]() |
![]() |
#10 |
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 |
![]() |
![]() |
![]() |
#11 | |
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:
__________________
_________________ |...............| We live in the nick of times. | Len 17, Wid 3 | |_______________| [pics] |
|
![]() |
![]() |
![]() |
#12 |
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 |
![]() |
![]() |
![]() |
#13 |
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] |
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|