473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Password Verification program help

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
9 11502
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
> 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
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
<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
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
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
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
I got it to work; thanks everyone for your help

Dec 6 '05 #9
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard M | last post by:
Can anyone please lend me a hand, I'm trying to design a cookie based password system. I have managed to do everything bar one thing that is driving me crazy. I have a MYSQL database called...
2
by: Rickard | last post by:
OK. I have the need on a form submit to pass the data to anther system that require a login and password only I want to automate the process in an server side asp file and then process the...
1
by: PerryC | last post by:
All, This should be very simple for you VB programmers, I am just a beginner and am stuck! It should be very simple... what am I not doing right? Dim PW As String PW = Me.Password If...
2
by: MLH | last post by:
What's the simplest way to allow a user of an A97 app to change password?
0
by: pwilliams | last post by:
NCOALink Change of Address Verification Each year over 40 million Americans change their mailing addresses. This change is equivalent to every person in California deciding to change addresses...
13
by: Kal | last post by:
I have a small console app that started out in dotnet 1.1 in VS 2003. That version can be copied to a W2K3 server where it runs fine. I set up a new project in VS 2005 and copied the code files...
3
by: Saqib Ali | last post by:
is there a way to enforce password history verification when using Directory.Entry.Invoke("SetPassword", newPasswdStr)??? saqib http://www.full-disk-encryption.net
1
by: wideasleep | last post by:
Hello Everyone, I have to set up a electronic form for recording additions to a chemical proccess where I work. Think of this as a big kitchen but instead of food products we have chemicals. So...
30
by: diane | last post by:
I've got an application running with table-based security: i capture the user's windows login with fOsusername, then have them enter a password checked against their username/login in my own table....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.