Hopefully you guys can be as helpful and fast as you were last time I asked for help!
So, I'm supposed to make a program that prompts the user for a username and password. It then checks the password string, and if it's under five characters or contains a space, makes them enter a different. Then it stores them both in a different string.
I have been trying to make the password verification thing for hours now and it's like I'm just slamming my dick in a door over and over again. Here's the function I have right now:
string getPassword ()
{ string pass;
int check = 0;
cout << "\n Please enter a password. password must have \n"
<< " at least five characters and may contain no \n"
<< " spaces.\n\n ";
getline(cin, pass);
do
{
getline(cin, pass);
if (pass.length() < 5)
{
cout << "\n\n The password you entered is invalid.\n"
<<" Please try again.\n\n ";
}
else
{
for (int i = 0; i < pass.length(); i++)
{
if (pass == ' ')
{
cout << "\n\n The password you entered is invalid.\n"
<<" Please try again.\n\n ";
}
else
{
if (i == pass.length())
{
return pass;
}
}
}
}
}
while (pass.length() <= 5);
To my eye I don't see why this shouldn't work. It does accurately catch passwords with spaces in them, but it doesn't return the pass variable to main when a valid password is entered. I can't figure it out.
Help would really be appreciated!
Posts
Also, don't you want to for(i=0;i<pass.length()-1;i++) ?
And just to be nitpicky, you have an extra variable that you don't use.
If I take out the getline(cin, pass) outside of the do while loop, then the loop executes and outputs the invalid password message without giving the user the opportunity to actually enter a password, I don't really get why
Your suggestion for the for loop doesn't appear to have fixed any thing
http://www.audioentropy.com/
edit: the reason i ask is it is probably going to be 1 more than you think since you start counting sring positions at 0 and length at 1.
it's occurred to me that there's no reason to have the return statement in the loop and that that's probably at least part of my problem. It makes more sense to check for both possible errors, and if one of them exists, repeat the loop, and if neither does, end the loop and return pass to main.
The problem I'm having now is figuring out some way to tell the do-while loop that one of the values for i in the for loop came back as a space.
http://www.audioentropy.com/
edit: Is there a reason you aren't just debugging the code with break points? The answer to the questions you've asked in this thread could be solved by walking through your program a single time. This would also probably give you an explanation on why a single cin inside the while loop doesn't work though I think it should.
edit2: Also can you not use the standard libraries cause there's a str function that allows you to search strings.
break;
}
The thing I was mainly getting at, was, are you sure that pass.length() is returning the exact length of the string, and not length-1?
There's a whole better way to do the loop, but I'm a bit tired right now and not thinking extremely clearly on it, which is why I pointed out the extra getline outside of the loop.
For something like this you could even run through it yourself with pencil and paper. Just go line by line, keeping track of the values of all your variables, and you should run across your problem. Helps take some of the mystery out of it.
The way I see it, you have two different tasks:
1) prompting the user for a password until you get a valid one
2) testing if a given string is a valid password
I would put those things in two functions. That simplifies your "read" function to this pseudocode:
- output prompt text
- read user input
- while userinput is no valid password:
---- output second prompt text
---- read user input
- endwhile
- return input string
in real code this could be something like:
The second task is also very simple in pseudocode:
- test string for length < 5
- for each letter in the string:
---- test if letter is ' '
- endfor
Again the code:
I think your holdup was defining a clear outer loop, because you had to test for two different things. You can of course break out of any loop with "return" or "break" but that makes the structure of the code more complicated and it's harder to see how you go through it.
Note: there's lots of ways to solve this problem and I just presented my personal approach. Also I don't guarantee there are no errors in the code, I didn't test this and I also didn't think more than 5 minutes about it. You should at least take a hard look at all the numbers in the test function and what value you get from string.length() etc.
Note2: You may have noticed that I renamed your String to password. I'm not an advocate of theseLongNamesThatManyCplusplusProgrammersUse, but that string is the most important thing in your code, so it should have a good name. Pass could e any number of things, but password makes the intention clear.
I also inserted a short comment at the beginning of the test function to tell the reader what it does. I didn't even really notice I did it, until I pasted the code here, it's a force of habit for me now. It's a good idea to do that even before you write the code. It helps you get a clear understanding of what the function is supposed to do and if you ever need to look at that code again you'll be thankful. You don't need to put a comment on every line and if you have good function names you only need sparse commenting anyway (it's pretty clear from the name what readPassword is supposed to do) but in some places a comment is just necessary.