![]() |
Quote:
Quote:
In a nutshell, this is how I check for two conditions: Quote:
|
Quote:
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: (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> |
Quote:
Quote:
Every time you call if (!a()), you are calling a(). Quote:
|
Quote:
Quote:
Quote:
|
double post
Quote:
|
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> |
Quote:
Either way you should at least store the values of a and b each time through the loop. |
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)) 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. |
Quote:
Code:
int index = 0; Code:
cout << "Passwords must include at least on digit (1-9)" << endl; 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:
|
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:
|
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.) |
Quote:
|
Quote:
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))) |
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. |
All times are GMT -5. The time now is 02:34 AM. |
Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.