kwind...@gmail.com wrote:
> This program doesn't seem like it should be too hard, but I cannot
> figure it out. Here is the assignment:
>
> Password Verifier - Write a program to verify passwords, satisfying the
> following specifications:
>
> ALGORITHM:
> 1. Ask the user to input a possible password.
> 2. Check to see that it:
> - is at least 8 characters long
> - is no longer than 16 characters
> 3. While it's not valid:
> - tell the user why it isn't valid
> - and go back to step one
> 4. Once it's valid, ask the user to re-enter it.
> 5. Compare the new password to the old one, and if it's NOT the same,
> ask again.
> HINT: Could be an inner loop that calls a function.
> 6. If the user has tried and failed to duplicate the password 3 times,
> start over, asking for a new password.
> 7. Once the user has created and verified a good password, give output
> saying so.
>
> SPECIFICATIONS:
> The program should implement the algorithm above.
> The program should contain 2 functions other than main that take
> parameters and return values.
> All output statements should belong to main and NOT the other
> functions.
> All calculation and/or comparison statements should exist in other
> functions - not main.
>
> -------------------------------------------------------------------------------------------------------------------------
>
> Here is what I have so far:
>
> #include <iostream.h>
Non standard, use <iostream>.
> #include <stdlib.h>
Prefer <cstdlib>
> #include <string>
> using namespace std;
http://www.parashift.com/c++-faq-lit....html#faq-27.5
> void enterPassword();
> void reenterPassword();
These are not descriptive names. enterPassword() does not enter the
password, it checks for its length. reenterPassword() does not reenter
the password, it checks if both match.
> string password1;
> string password2;
These could easily be avoided by making them local to main() and
passing them to the functions. Wasn't that part of the requirements
anyways?
> int password1_length;
This one should definitely be local to enterPassword().
> bool valid_length = false;
> bool passwords_match = false;
These should be returned by the functions. Part of the requirements
again.
> int main()
> {
> do
> {
> cout << "Enter a password: ";
> cin >> password1;
> enterPassword(); //function call
This could be
enterPassword(password1);
> }
> while (valid_length = false);
Watch out! = is not ==. The first one assigns, the second one tests.
Here, you assign false to valid_length and test it, yielding always
false, the loop always terminating.
This could be
while (enterPassword(password1));
if enterPassword() returns a bool describing the validity of the input.
> do
> {
> cout << "Re-enter your password: ";
> cin >> password2;
> reenterPassword(); //function call
> }
> while (passwords_match = false);
Same thing here, for the = and the tests.
> system("PAUSE");
Watch out. system() is standard, but what goes in it is not. On my
system, the PAUSE command freezes the whole computer and I have to flip
a switch located in the next room to make it work again.
> return 0;
> }
>
>
> void enterPassword() //function definition
> {
> password1_length = password1.length();
This should be
std::size_t password1_length = password1.length();
Globals are used when they need to be accessed in more than one
function and you are too lazy (or not competent enough) to use other
safer means.
> if ( password1_length >=8 && password1_length <= 16 )
> {
> valid_length = true;
> }
>
> else
> {
> valid_length = false;
> }
Return the result instead so it can be tested.
> }
>
>
>
> void reenterPassword() //function definition
> {
> if ( password1 != password2 )
> {
> passwords_match = true;
> }
>
> else
> {
> passwords_match = false;
> }
Same here, return the result.
> }
> -------------------------------------------------------------------------------------------------------------------------
>
> Questions:
>
> 1. When I run the program, it seems like the other functions are not
> being accessed. Have I defined and/or called the functions properly?
Yes, but you failed to compare the results correctly. Learn to use your
debugger to see what's happening and get a good book explaining the
basics of C++.
> 2. Where do I place the "invalid password" outputs in the main
> function?
1) ask the input
2) check the input
3) if the input is bad, print a message and loop
4) if the input is ok, break from the loop
> 3. Where does the for loop go that will count the number of times the
> user has entered the password incorrectly?
You could replace the second while loop by a for loop, from 0 to 2
inclusively.
Jonathan