![]() |
|
Technology Computing, programming, science, electronics, telecommunications, etc. |
![]() |
|
Thread Tools | Display Modes |
![]() |
#1 | |
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
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:
__________________
****************** 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 |
|
![]() |
![]() |
![]() |
#2 | |
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
This illustrates my dilemma:
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 |
|
![]() |
![]() |
![]() |
#3 |
Turns out my CRS is a symptom of TMB.
Join Date: Jan 2010
Location: Chicago suburbs
Posts: 2,916
|
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); }
__________________
![]() ![]() ![]() Talk nerdy to me. |
![]() |
![]() |
![]() |
#4 | |
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
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:
__________________
****************** 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 |
Person who doesn't update the user title
Join Date: Jun 2010
Location: Bottom lands of the Missoula floods
Posts: 6,402
|
Don't you need a way out of the loop if the User wants to "Cancel".
|
![]() |
![]() |
![]() |
#6 |
Turns out my CRS is a symptom of TMB.
Join Date: Jan 2010
Location: Chicago suburbs
Posts: 2,916
|
No. Step through the code and ask yourself what would happen if the password were too short and had no digit.
@Lamplighter - yes in a real world program. Flint didn't mention it as part of the exercise.
__________________
![]() ![]() ![]() Talk nerdy to me. |
![]() |
![]() |
![]() |
#7 | ||
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
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?
__________________
****************** 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-22-2010 at 02:31 PM. |
||
![]() |
![]() |
![]() |
#8 |
I think this line's mostly filler.
Join Date: Jan 2003
Location: DC
Posts: 13,575
|
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 bool containDigit(string) // just print error, and return true/false. Don't read a new pw string getPW() { string pw = read line; while (( ! passlength(pw) ) || ( ! containDigit(pw) )) { pw = read line; } return pw; }
__________________
_________________ |...............| We live in the nick of times. | Len 17, Wid 3 | |_______________| [pics] Last edited by Happy Monkey; 11-22-2010 at 02:55 PM. Reason: logic error |
![]() |
![]() |
![]() |
#9 |
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
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.
__________________
****************** 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
|
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.
__________________
****************** 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 | |
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
Okay, now THIS version works right, every time, under every condition.
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 |
|
![]() |
![]() |
![]() |
#12 | ||||
Snowflake
Join Date: Mar 2006
Location: Dystopia
Posts: 13,136
|
This is somewhat better, as far as a more streamlined main function...
Quote:
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 |
||||
![]() |
![]() |
![]() |
#13 |
I think this line's mostly filler.
Join Date: Jan 2003
Location: DC
Posts: 13,575
|
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.
__________________
_________________ |...............| We live in the nick of times. | Len 17, Wid 3 | |_______________| [pics] |
![]() |
![]() |
![]() |
#14 | |||
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 |
|||
![]() |
![]() |
![]() |
#15 |
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] |
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | |
|
|