C++ Help, Please. Thank You.
This program is supposed to check that a password is at least 6 characters AND contains at least one digit. What I never was able to figure out is how to check for BOTH conditions. Since I have them in two functions, once one is satisfied, it drops to the next one and forgets about the other. I didn't know how to get "back" to the previous condition, so I copy/pasted the code in an if/else statement! The if statement was originally supposed to print the "Thank you that is a valid password" but it didn't print, so I put it in again at the bottom.
However, if you put in a correct password at the first prompt, the if statement prints. In the version I am turning in, I told it to print an endl; (it just skips a line). See how the "Please enter a password: ";s are numbered 1 through 4? if you get down to "Please enter a password4: "; and enter 123 it accepts this as valid. It still does not check "also" for length. I don't know how to validate a string of input for two separate conditions. The problem is, when it fails I need it to tell the user why it failed, so I can't just lump them together. I am going cross-eyed. Can anybody tell me what my ignorant mistake is? There are no examples of this in my textbook... Quote:
|
This illustrates my dilemma:
Quote:
|
What you want to do is to check the minimum number of times.
bool oklength = PassLength(password); bool okdigit = containDigit(password); while (!okLength || !okDigit) { if (!okLength) // err MSG if (!okDigit) // err MSG // get password again oklength = PassLength(password); okdigit = containDigit(password); } |
Thank you. I came to more or less the same conclusion.
All last night, I was dreaming in flowcharts. I was continuously, acutely aware that the subject of each dream was being passed as a parameter to the dream function. For a nice dose of recursion, I think the only dream I had was the one in which the above happened. When I woke up this morning I was thinking this: Quote:
|
Don't you need a way out of the loop if the User wants to "Cancel".
|
Quote:
@Lamplighter - yes in a real world program. Flint didn't mention it as part of the exercise. |
Quote:
Quote:
Once inside the loop, it should keep prompting and re-executing the loop until "false OR false" is false. ... If I step through using "abc" as the input (too short and has no digit), it would say too short and get a new password. Then evaluate for digits. Then evaluate the loop again. Right? |
I think yours would return a valid password, but [edit]
Code:
bool passLength(string) // just print error, and return true/false. Don't read a new pw |
Oh, I see. Put the error message in the function. Well, yes, they always go together.
Thank you, all. For your help here I will send you a singing telegram from a juggling midget hooker on a unicycle, and/or make a modest donation to the Cellar tip jar. My choice. |
Still haven't got it quite worked out, but I see that what I turned in has been graded and I got a 100. So... no rush, but I'm still going to keep at this until it works exactly right every time, under every circumstance.
|
Okay, now THIS version works right, every time, under every condition.
Quote:
|
This is somewhat better, as far as a more streamlined main function...
Quote:
Quote:
Quote:
Quote:
|
You are reading new passwords all over the place. What if the password you read in passLength() is good? You return false anyway and ask for a new one at the next step.
You should only do one getline() per loop. |
Quote:
Quote:
Quote:
|
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 |
All times are GMT -5. The time now is 10:24 AM. |
Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.