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

Parallel Arrays in Bool function (diff. part of pw manager program)

$
0
0
Ok so I'm stuck on a different part of the password manager assignment. For this part we are suppose to read the logins and passwords for existing users from file "password.txt". Then we are suppose to ask the user for their login name (have to use string type to store login name). Then we have to verify that the user exists within the file using function called searchUserInfo which has three arguments: one for the login name, one for number of users, and a location index representing where the search item is in the array (location index has to be pass by reference). Then, if the user does exist the program prompts for them to enter their password. Up to that the program works. The problem I am having is the next part where we have to verify the password using same searchUserInfo function by using the concept of parallel arrays. I just can't figure out how I am suppose to check the password in the function using parallel arrays! We were only given one example of parallel arrays and it did not have functions nor did it use strings or boolean variables. Here is the code so far: (oh and I have to use the global pointers even though I don't want to so that's why those are there):
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#include<fstream>

using namespace std;
// function prototypes
void displayMenu();
int getMenuChoice();
int readCredential();
bool searchUserInfo(string, int, int &);

//Global string pointers hold passwords
//and login names stored in "password.txt"
string *pwInFile;
string *lgnInFile;
int main()
{
	const int CHOICE_ONE = 1,
	          CHOICE_TWO = 2,   //the code I'm posting here is just for CHOICE_TWO
		  CHOICE_THREE = 3,
		  CHOICE_FOUR = 4;
        int choice;                       //stores menu choice
        int fSize;                        //stores size of file (aka the number of users)
        string userName,                  //stores userName for choice 2
	       pw;                        //stores password for choice 2
	int pos;                          //stores position of login and password in respective arrays
	bool lgnValid;                    //stores truth value returned by searchUserInfo


	// Displays menu
	displayMenu();
	
	// Get's menu choice and assigns it to main int variable choice
	choice = getMenuChoice();
	
	cin.ignore(100, '\n');
        
        switch(choice)
	{
		case CHOICE_ONE:
                    break;
                case CHOICE_TWO:
			fSize = readCredential();  // stores num of users into fSize variable
			
                        // Prompts user for login name
			cout << "Enter your login name: " << endl;
			getline(cin, userName);
			
			// stores result of searchUserInfo in lgnValid variable
			lgnValid = searchUserInfo(userName, fSize, pos);
                        
                        // if lgnValid is false produce error message
                        // otherwise ask for user's current password and
                        // check if it is valid. If pw not valid then
                        // exit program, otherwise continue through choice 2.
			if(lgnValid == false)
			{
				cout << "Error: this login is invalid!" << endl;
				exit(0);
			}
                        else
			{
				cout << "For validation, enter your current Password: " << endl;
				getline(cin, pw);

                                // function call to searchUserInfo for pw will go here once
                                // it's working
                        }
                        break;
	        case CHOICE_THREE:
		
			break;
		case CHOICE_FOUR:
			
			break;
	}
	
	return 0;
}
//*******************************************************
// Function definition for displayMenu function.        *
// Displays menu.                                       *
//*******************************************************
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 getMenuChoice function       *
// Gets menu choice form user and checks whether or     *
// not it is valid.                                     *
//*******************************************************
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 readCredential function.     *
// This function reads through file, determines number  *
// of users in the file and returns that number.        *
// This function also stores the passwords in the       *
// global pointer variable pwInFile and the login names *
// in the global variable lgnInFile.                    *
//*******************************************************
int readCredential()
{
	ifstream pwFile;              // variable for input file
	int index = 0;                // counter variable for counting users in file
	int fileSize;                 // number of users to be returned
	string pw;                    // holds items in password.txt for counting loop
	
	// Open file
	pwFile.open("password.txt");
	
	if(pwFile)
	{
	// Reads data in file then counts how many users are in file
	while(getline(pwFile, pw))
	{
		index++;
	}
	// Store number of users in variable fileSize
	fileSize = index;
	}
	// Print error message if file does not open correctly
	else
	{
		cout << "Error: could not open file!" << endl;
		exit(0);
	}
	//close file
	pwFile.close(); 
	
	// Allocate new memory for lgnInFile and pwInFile
	lgnInFile = new string[fileSize];
	pwInFile = new string[fileSize];
	
	// Open file again
	pwFile.open("password.txt");

	// Store all data in lgnInFile array
	for (int count = 0; count < fileSize; count++)
        {	
		getline(pwFile, lgnInFile[count]);
	}
	// new count for new while loop
	int count = 0;

	// separates the login names and passwords placing them in their
	// respective arrays. 
	while(count < fileSize)
	{
		int l = lgnInFile[count].length();
		int p = lgnInFile[count].find(' ');
		pwInFile[count] = lgnInFile[count].substr((p+1), l);
		lgnInFile[count].erase(p, 9);
		count++;
	}

	// closes file 
	pwFile.close();
	// returns number of users in file
	return fileSize;
}
//*******************************************************
// Function definition for searchUserInfo function      *
// Searches file and determines if username (and        *
// password which is what I am having trouble figuring  *
// out how to do) and returns true if they are in the   *
// file or it returns false if they are not in the file.*
//*******************************************************
bool searchUserInfo(string uName, int numUsers, int &l_index)
{
	int count = 0;  //counter variable
	bool vldLgn;    //truth value to be returned
	
	while(count < numUsers)
	{
		// Compares user entered login name with
		// usernames in file to determine if user 
		// is already in file
		lgnInFile[count].compare(uName);
		// Not sure what to do with this to make it
		// compare password instead of username
		// only when I need it to. 
		pwInFile[count].compare(uName);
		
		// If lgnInFile[count].compare(uName) is 0
		// then vldLgn is true, count is assigned to
		// l_index to get position of username
		// and loop ends.
		if (lgnInFile[count].compare(uName) == 0)
		{
			vldLgn = true;
			l_index = count;
			break;
		}
		// else if(!lgnInFile[count].compare(uName))
		// If lgnInFile[count].compare(uName) is not 0 then
		// vldLgn is false.
		else
		{
			vldLgn = false;
		}
	
		// increment count
		count++; 
	}

	return vldLgn;
}



And here are the contents of the file "password.txt":
/*
Patrick patr1C|<
SpongeBob patr1C|<
Squidward jellY?#!
Sandy #Air911#
Plankton Krabby9*
Krabs M0ney$!$
animefan ElrIc67&
*/



I've looked through my book and spent hours trying to look up things about parallel arrays but I still can't figure out how to use searchUserInfo to validate the user's password after using the function to check whether the username they entered was in the file or not. Any help and advice is appreciated and examples are very much appreciated. Thanks in advance for any help.

Also, if I have any more problems with this assignment before it's due I will post the questions and sections of code I'm having problems with on this topic. It's a big assignment so it is possible that I will need help with another section further on and any future help is greatly appreciated as well.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



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