Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

C++ Password Manager prgm error check does not work correctly

$
0
0
I am currently working on a password manager program that is suppose to get login name and password from people who want to create new ones and allow users who already have username and login in file to change their current login and password among other thins. I am currently having problem with error checking the password when someone is creating a new password.
These are the instructions for the part of the assignment that I am currently working on:
/*3.1 Ask and get login name from user: [2]
Display the following prompt: "Enter the login name you would like to use
(all characters with no spaces): "
3.2 Ask and get new password from user: [2]
Display the following prompt:
"Enter the Password you would like to use (Must be exactly 8
characters long including at least one of each (uppercase, lowercase,
digit, special character): "
NOTE: To store the password, you must use C-string (character array).
DO NOT USE string type.
3.3 Check validity for password: [16]
After you read in the user input, your program will need to check constraints and assess validity
of the password. (Hint: you can access each element in the C-string character array to check
validity)
A password is invalid if:
(1) it is same as the login name
(2) it is not 8 characters long
(Hint: use strlen, strcmp functions of C-string class).
(3) it does NOT meet the following criterion:
1. has one or more uppercase letters;
2. has one or more lowercase letters;
3. has one or more numbers;
4. has one or more special symbols, such as ` ! " ? $ ? % ^ & * ( ) _ - + = { [ ] : ; @ '
To check this constraint, write a function countUnits that will take as input a pointer to character
(the C-string password) and integer counters for uppercase, lowercase, digit, special symbols
passed as reference. It won’t return anything. The function will test each character unit to count
the number of digits, upper and lower cases and all other special symbols in the array.
HINT: you can use both isprint and ispunct functions to identify all special symbols
other than letters or numbers. Look into how pointers can be used to parse C-strings.
Your program should not accept invalid password and if invalid, display appropriate message
for all three invalid cases and repeat steps 3.2 and 3.3.
3.4 Save password to file: [4]
If the password is valid, your program should add both the login name and the new password in
a text file named (“password.txt”).
HINT: you will need to open the password.txt as output file object in append mode.
Display the following:
"Your credential is added!"*/


It will do the error check correctly for the first couple of times and then after I've entered an incorrect password a few times it says it's correct even if it isn't and adds it to file called password.txt (I'm not using the contents yet so that's why they are not included in this post). I can't figure out what's going wrong. Here is my code so far (btw I haven't implemented the menu into the program yet so that's why that isn't there):
#include<iostream>
#include<string>
#include<cstring>
#include <cstdlib>
#include<fstream>

using namespace std;

void displayMenu();
int getMenuChoice();
char *getLoginName(int);
char *getPassword(int);
void countUnits(char *, int, int &, int &, int &, int &);
bool comparePW_Login(char *, char*, int);
bool isPWLengthValid(char *, int);
bool isPWValid(char *, char *, int, int &, int &, int &, int &);
void savePW_LGNtoFile(char *, char *, int, int);
void menuChoice1();

int main()
{
	int const SIZE = 9;
	int const SIZE2 = 100;
	int lgInSize;
	char *password,
		 *loginName,
		 *password2;        // to hold new password if error w/ first then copy into password
	int choice;
	int count = 0;
	int uCount = 0,
		lCount = 0,
		dCount = 0,
		sCount = 0;
	bool cmpPw_lgn,
		 pwLngth,
		 pwVld;
		 
	
	// Displays menu
	displayMenu();
	
	// Get's menu choice and assigns it to main int variable choice
	choice = getMenuChoice();
	
	cin.ignore(100, '\n');
	
	// Test getLoginName function
	loginName = getLoginName(SIZE);
	cout << "Login name you entered is: " << loginName << endl;
	
	do{
	password = getPassword(SIZE);
	cout << "Password you entered was: " << endl;
		
	while (password[count] != '\0')
		{
			cout << password[count];
			count++;
		}
	cout << endl;
	cout << endl;
	
	// Function call to countUnits
	countUnits(password, SIZE, uCount, lCount, dCount, sCount);
	cmpPw_lgn = comparePW_Login(password, loginName, SIZE);
	pwLngth = isPWLengthValid(password, SIZE);
	pwVld = isPWValid(password, loginName, SIZE, uCount, 
					  lCount, dCount, sCount);
	

	if(cmpPw_lgn == false)
	{
		cout << "Cannot be same as your login name!" << endl;
	}
	else if(pwLngth == false)
	{
		cout << "Must be exactly 8 characters long!" << endl;
	}
	else if(uCount == 0 || lCount == 0 || dCount == 0 || sCount == 0)
	{
		cout << "Violates the Character Rules!" << endl;
	}
	else
	{
		break;
	}
	}while(pwVld == false);
	
	savePW_LGNtoFile(password, loginName, SIZE, SIZE2);
	
	cout << "Your credential is added!" << endl;
	cout << endl;
	
	

	delete [] loginName;
	delete [] password;
	loginName = 0;
	password = 0;
	return 0;
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
void displayMenu()
{
	cout << "Password Manager Menu: " << endl;
	
	cout << "1: Create Password for New User" << endl;
	cout << "2: Change Password" << endl;
	cout << "3: Remind Password" << endl;
	cout << "4: Verify Password" << endl;
	cout << endl;
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
int getMenuChoice()
{
	int choice;
	
	cout << "Enter choice here: " << endl;
	cin >> choice;
	
	while (choice < 1 || choice > 4)
	{
		cout << "You have entered an invalid menu choice." << endl;
		cout << "Please enter an number between 1 and 4: " << endl;
		cin >> choice;
	}
	
	return choice;
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
char *getLoginName(int size)
{
	char *loginPtr;
	
	loginPtr = new char[size];
	cout << "Enter the login name you would like to use"
		 << "(all characters with no spaces): " << endl;
	cin.getline(loginPtr, size);
	
	
	return loginPtr;
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
char *getPassword(int size)
{
	//char pwArray[size];
	char *pwPtr;
	
	pwPtr = new char[size];
	cout << "Enter the password you would like to use "
	     << "(Must be exactly 8 characters long including "
		 << "atleast one of each (uppercase, lowercase, digit, "
		 << "special character)): " << endl;
	cin.getline(pwPtr, size);
	
	 
	return pwPtr;
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
void countUnits(char *pwPtr, int size, int &upperCount, int &lowerCount, int &digitCount, int &spSymCount)
{
	int count = 0,
	    count2 = 0,
		count3 = 0,
		count4 = 0;
	
	while(pwPtr[count] != '\0')
	{
		if(isupper(pwPtr[count]))
			upperCount += 1;
		count++;
	}
	 
	while(pwPtr[count2] != '\0')
	{
		if(islower(pwPtr[count2]))
			lowerCount += 1;
		count2++;
	}
	
	while(pwPtr[count3] != '\0')
	{
		if(isdigit(pwPtr[count3]))
			digitCount += 1;
		count3++;
	}
	
	while(pwPtr[count4] != '\0')
	{
		if(ispunct(pwPtr[count4]))
			spSymCount += 1;
		count4++;
	}
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
bool comparePW_Login(char *pwPtr, char *loginPtr, int size)
{
	int count = 0;
	
	if(!strcmp(pwPtr, loginPtr))
		return false;
	else
		return true; 
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
bool isPWLengthValid(char *pwPtr, int size)
{
	int length;
	int count = 0;
	
	length = strlen(pwPtr);
	
	if(length < 8 || length > 8)
		return false;
	else
		return true;
	
}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
bool isPWValid(char *pwPtr, char *loginPtr, int size, int &upCount, int &lwrCount, int &dgtCount, int &syCount)
{
	bool valid1 = isPWLengthValid(pwPtr, size);
	bool valid2 = comparePW_Login(pwPtr, loginPtr, size);
	// think of better way to do this so not long list
	if (valid1 == false || valid2 == false || upCount < 1 || lwrCount < 1 || dgtCount < 1 || syCount < 1)
		return false;
	else
		return true;

}
//*******************************************************
// Function definition for 
//
// 
//*******************************************************
void savePW_LGNtoFile(char *pwPtr, char *lgnPtr, int size, int size2)
{
	//int index = 0;
	ofstream pwFile;
	pwFile.open("password.txt", ios::app);
	
	if(!pwFile)
	{
		cout << "Error opening file!" << endl;
		exit(0);
	}
	else
	{
		//for(int index = 0; index < size; index++)
		while(*lgnPtr != '\0')
		{
			pwFile << *lgnPtr;
			lgnPtr++;
		}
		pwFile << " ";
		//index = 0; //resets index to 0
		//for(int index = 0; index < size2; index++)
		while(*pwPtr != '\0')
		{
			pwFile << *pwPtr;
			pwPtr++;
		}
		cout << endl;
	}
	
	pwFile.close();
}


I've attached some example outputs to illustrate what's going wrong. Thanks in advance for the help!


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>