write a program that will verify passwords. A valid password consists of: At lea
ID: 3681712 • Letter: W
Question
write a program that will verify passwords. A valid password consists of:
At least 6 characters
At least one uppercase letter
At least one lowercase letter
At least one digit
At least one of 4 special characters: $ % # *
Each password will be processed one character at a time. Each character should be printed on the output screen. Once a password has been processed, display whether or not the password was valid, and for the invalid passwords, display all of the things that were wrong with it.
Using an Input File in a CPP program
The input file can be downloaded and saved on your computer. It is called password.txt (below is the contents of the file):
Since the input for this program will be coming from a text file rather than standard input (the keyboard), the program will not use cin and the input operator when it needs to read data. Instead, it will have to read the data from the file.
In order to read from a file, create an input file stream variable and "connect" it with the text file. The "connection" is created by opening the file:
The open statement will open a file named password.txt. The file must be located in the same directory as the CPP file.
Once the file has been opened, make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly:
In order to read a character from a file, the input operator could be used in the same manner that it's used with a cin statement. So:
However, that would not work for this program because it skips over whitespace characters (specifically newline characters) and we need to be able to process those to find the end of a password. So for this program, use the get function to read a single character:
Since the data is coming from a file, there has to be some way to determine when there is no more data. One way to do that is to use the input file stream variable as a boolean variable:
As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is only successful after an attempt has been made to read data from the file. This means that a standard read loop pattern should be followed.
When a file is done being processed, it should be closed.
Processing:
For this program, the text file (password.txt that was mentioned above) will be processed one character at a time. To do this, a set of nested loops will be needed. The outer loop will execute as long as there are characters in the file. The inner loop will process each individual password. So the basic logic for the program is as follows:
Note: One thing that has been left for you to figure out is how best to keep track of the things that are or are not wrong with an individual password. Whatever method is chosen, don't forget that there are multiple passwords in the file. This means that somewhere in the outer loop, don't forget to reset whatever you have chosen back to a state for a new password.
Functions:
This program will involve using some pre-written library functions and one that you design yourself.
Library Functions
The following set of functions are contained in the cctype library and can simply be called in the program (ie. they do not have to be written): isalpha, isupper, islower, isdigit.
Each of the "is" functions take a character as an argument and returns a true or false value:
isalpha will return true if the passed in character is an alphabetic character; and false if the passed in character is not an alphabetic character
isupper will return true if the passed in character is an uppercase alphabetic character; and false if the passed in character is not an uppercase alphabetic character
islower will return true if the passed in character is a lowercase alphabetic character; and false if the passed in character is not an lowercase alphabetic character
isdigit will return true if the passed in character is a digit character; and false if the passed in character is not a digit character
The return value for the "is" functions allows for the functions to be called as the condition in an if statement:
Other Function
The function listed below is the ones that you will be required to write for the assignment.
bool isSpecial( char ch )
This function will test if a character is a $, %, # or *. It takes as its argument: a single character that represents the character to be tested. It returns true if the passed in character is a special character or false if the character is not a special character.
Program Requirements:
Add #include statements for the cctype, fstream, and cstdlib libraries to the top of the code.
Hand in a copy of your source code using Blackboard.
Output:
Using the password.txt file mentioned above produces the following output:
Notes
Above, it says that isalpha() and the other "is" functions return a true or false value. That's part of the truth. If you look up the functions, you will see that they actually return an integer. As mentioned in lecture, any non-zero value is considered true; whereas 0 is considered false.
Explanation / Answer
/***** c++ code that will verify passwords. ******/
#include <iostream>
#include <cstring> // for std::strlen
#include <cctype>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;
bool isSpecial(char ch)
{
bool rval = false;
int size = 4;
char symbols[] = {'#', '%', '$', '*'};
for (int i = 0; i < size; ++i) {
if (symbols[i] == ch)
rval = true;
}
return rval;
}
/*DEFINITION OF FUNCTION TEST_PASS:
This function uses a for loop combined
with an if-else statement to test whether
or not all of the requirements were met
for the password. The parameter char pass[]
represents the array being passed into the
function (the string holding the password).
Additionally, four boolean values have been
created to test the password for different
components.*/
bool Test_Pass(string pass)
{
// Boolean variables.
bool alength = false,
aUpper = false, // For uppercase characters.
aLower = false, // For lowercase characters.
aDigit = false, // For digits.
aspecial = false; // For special.
for ( int i = 0 ; i < pass.size() ; ++i )
{
if (isupper(pass[i])) // Check whether there exists at least one uppercase character.
aUpper = true ;
else if (islower(pass[i])) // Check whether there exists at least one lowercase character.
aLower = true ;
else if (isdigit(pass[i])) // Check whether there exists at least one digit character.
aDigit = true ;
else if (isSpecial(pass[i])) // Check whether there exists at least one special character.
aspecial = true ;
}
if ( aUpper && aLower && aDigit && aspecial )
return true;
if (!aUpper)
{
cout << "Missing Uppercase letter ";
}
if (!aLower)
{
cout << "Missing lowercase letter ";
}
if (!aDigit)
{
cout << "Missing digit ";
}
if (!aspecial)
{
cout << "Missing special character ";
}
return false;
}
int main()
{
int total = 0;
int valid = 0;
int invalid = 0;
string password;
ifstream infile; //input file stream variable
//this will be used instead of cin
infile.open( "1.txt" ); //open the file for reading
if( infile.fail() ) //if the input file failed to open
{
cout << "input file did not open" << endl;
return 0;
}
else
{
while( infile >> password)
{
cout << password << endl;
int length = strlen(password.c_str());
if (length < 6) cout << "Not enough characters ";
if (Test_Pass(password.c_str()) == true)
{
cout << "Valid Password" << endl;
valid++;
}
else invalid++;
cout << endl;
total++;
}
cout << "Total Number of Passwords: "<< total << endl;
cout << "Valid: "<< valid << endl;
cout << "Invalid: "<< invalid << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.