By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

Password Verification program help

P: n/a
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>
#include <stdlib.h>
#include <string>
using namespace std;

void enterPassword();
void reenterPassword();

string password1;
string password2;
int password1_length;
bool valid_length = false;
bool passwords_match = false;

int main()
{
do
{
cout << "Enter a password: ";
cin >> password1;
enterPassword(); //function call
}
while (valid_length = false);
do
{
cout << "Re-enter your password: ";
cin >> password2;
reenterPassword(); //function call
}
while (passwords_match = false);
system("PAUSE");
return 0;
}
void enterPassword() //function definition
{
password1_length = password1.length();

if ( password1_length >=8 && password1_length <= 16 )
{
valid_length = true;
}

else
{
valid_length = false;
}
}

void reenterPassword() //function definition
{
if ( password1 != password2 )
{
passwords_match = true;
}

else
{
passwords_match = false;
}

}
-------------------------------------------------------------------------------------------------------------------------

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?

2. Where do I place the "invalid password" outputs in the main
function?

3. Where does the for loop go that will count the number of times the
user has entered the password incorrectly?
I am not asking someone to write the program for me, just for some
direction.

Dec 6 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

Dec 6 '05 #2

P: n/a
> 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.


I tried making them local but then the variables are undeclared in the
other functions.

Dec 6 '05 #3

P: n/a
kw******@gmail.com wrote:
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.


I tried making them local but then the variables are undeclared in the
other functions.


That's exactly what locals are for.

// using globals
int i;

void f()
{
i = 2;
}

int main()
{
f();
std::cout << i;
}
// using parameters
void f(int &i)
{
i = 2;
}

int main()
{
int a=0;
f(a);
std::cout << a;
}
Jonathan

Dec 6 '05 #4

P: n/a
<kw******@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.
Both of your functions return void. When he says return something he does
not mean void. He also says they take parameters. Your functions do not.
So get the shell or skeleton of your program to pass these superficial tests
before you start to debug it. Because, after you debug what you have here,
you will *still* have to write a program to meet those two constraints.. And
then debug that program too!
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>
#include <stdlib.h>
#include <string>
using namespace std;

void enterPassword();
void reenterPassword();


No return value. No parameters. No good.

It doesn't really jump out at me what he expects for the two functions.
But the one called by main might be:
bool get_password(string& passwd);
where the return value indicates success or failure.

And I suppose diddle around and try to use the other one somehow in the
retry mechanism..The need for the second one seems a bit forced to me. Note
that main will be almost empty, I consider that good design.
Dec 6 '05 #5

P: n/a
Here is what I have now:

#include <iostream.h>
#include <stdlib.h>
#include <string>
using namespace std;

int verifyLength();
int matchPasswords();

int valid_length = 0;
int passwords_match = 0;

int main()
{

string password1;
string password2;
int password1_length;
int number_tries;

do
{
do
{
cout << "Enter a password: ";
cin >> password1;
valid_length = verifyLength(password1, password2);
//function call
}
while (valid_length != 1);

number_tries = 0;

do
{
number_tries++;
cout << "Re-enter your password: ";
cin >> password2;
passwords_match = matchPasswords(password1, password2);
//function call
}
while (!passwords_match && number_tries < 3);

cout << "You have created a good password.";

}
while(!passwords_match);
system("PAUSE");
return 0;
int verifyLength() //function definition
{
int password1_length;
string password1;

password1_length = password1.length();

if ( password1_length >=8 && password1_length <= 16 )
{
valid_length == 1;
}

else
{
valid_length == 0;
}
}
int matchPasswords( string password2, string password1)
//function definition
{
if ( password1 == password2 )
{
return 1;
}

else
{
return 0;
}

}

Dec 6 '05 #6

P: n/a
Ian
kw******@gmail.com wrote:
enterPassword(); //function call
reenterPassword(); //function call
void enterPassword() //function definition
void reenterPassword() //function definition


To add to the sound responses already posted, what value to these
comments add?

Use comments for why and meaningful names for what.

Ian
Dec 6 '05 #7

P: n/a
Here is what I have now:

#include <iostream.h>
#include <stdlib.h>
#include <string>
using namespace std;

int verifyLength();
int matchPasswords();

int valid_length = 0;
int passwords_match = 0;

int main()
{

string password1;
string password2;
int password1_length;
int number_tries;

do
{
do
{
cout << "Enter a password: ";
cin >> password1;
valid_length = verifyLength(password1, password2);
//function call
}
while (valid_length != 1);

number_tries = 0;

do
{
number_tries++;
cout << "Re-enter your password: ";
cin >> password2;
passwords_match = matchPasswords(password1, password2);
//function call
}
while (!passwords_match && number_tries < 3);

cout << "You have created a good password.";

}
while(!passwords_match);
system("PAUSE");
return 0;
int verifyLength() //function definition
{
int password1_length;
string password1;

password1_length = password1.length();

if ( password1_length >=8 && password1_length <= 16 )
{
valid_length == 1;
}

else
{
valid_length == 0;
}
}
int matchPasswords( string password2, string password1)
//function definition
{
if ( password1 == password2 )
{
return 1;
}

else
{
return 0;
}

}

Dec 6 '05 #8

P: n/a
I got it to work; thanks everyone for your help

Dec 6 '05 #9

P: n/a
Ian
kw******@gmail.com wrote:
int verifyLength() //function definition
{ Of what? There are no parameters passed in, should this be password1?
int password1_length;
string password1;

password1_length = password1.length();

if ( password1_length >=8 && password1_length <= 16 )
{
valid_length == 1;
}

else
{
valid_length == 0;
} You don't return anything. }

int matchPasswords( string password2, string password1) Use bool.
//function definition drop this! {
if ( password1 == password2 )
{
return 1; true }

else
{
return 0; false }

}

or more simply, return (password1 == password2);

Ian
Dec 6 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.