So i am having an issue with a hashtable functions. In the main function the hashtables functions(removeitem, insertItem, and printTable) work with no issue but when i attempt to call them within another function AdminMenu they do not seem to work. For example when I run the program i can get the AdminMenu with no issues but once in the Admin menu a selection of an option produces the cout statement but does not complete the function. For example when you select 3 to print table nothing is output but the number 3. This function works when the function is called at the start of main but calling it inside the nested switch case adn it does not preform the function of printTable just outputs the selection 3.
#include<list>
#include<limits>
#include<string>
using namespace std;
// GLOBAL VARIABLES
bool empMatch= false; //set to false
bool keyExists = false;//set to false
//HASH TABLE CLASS STRUCT
class HashTable{
private:
static const int hashGroups = 10; //total size of 10 can be increased for larger groups
list<pair<int, string>> table[hashGroups];
public:
bool isEmpty();
int hashFunction(int key);
void insertItem(int key, string value);
void removeItem(int key);
bool searchTable(int key,string value);
void printTable();
};
//HASH FUNCTION TO CHECK IF TABLE EMPTY
bool HashTable::isEmpty()
{
int sum{};
for(int i{}; i< hashGroups; i++){
sum += table[i].size();
}
if(!sum){
return true;
}
return false;
}
//HASH FUNCTION
int HashTable::hashFunction(int key){
return key % hashGroups;
}
//HASH FUNCTION FOR INSERT VALUE
void HashTable::insertItem(int key, string value){
int hashValue = hashFunction(key);
auto& cell = table[hashValue];
auto tItr = begin(cell);
for(; tItr != end(cell); tItr++){
if(tItr->first==key){
keyExists = true; //if key exists replace value
tItr->second = value; //becomes value of input
cout<<"Warning key exists. value replaced\n";
break;
}
}
if (!keyExists){// If key does not exist then put back key and value
cell.emplace_back(key,value);
}
return ;
}
//HASH FUNCTION REMOVE
void HashTable::removeItem(int key){
int hashValue = hashFunction(key);
auto& cell = table[hashValue];
auto tItr = begin(cell);
for(; tItr != end(cell); tItr++){
if(tItr->first==key){
keyExists = true;
tItr = cell.erase(tItr);
cout<<"item removed\n";
break;
}
}
if(!keyExists){
cout<<"warning key not found";
}
return;
}
//HASH SEARCH TABLE FUNCTION FOR USER INPUT KEY AND USER INPUT VALUE
bool HashTable::searchTable(int key, string value){
int hashValue = hashFunction(key); //hash function to key input
auto& cell = table[hashValue];
auto tItr = begin(cell); //iterator
for(; tItr != end(cell); tItr++){
if(tItr->first==key){
keyExists = true;
if(keyExists){
if(tItr->second==value){//tests value of input is matching
empMatch=true;
}
else{
cout<<"access denied\n";
empMatch=false;
}
}
if(!keyExists){
cout<<"warning key not found\n";
empMatch= false;
}
}
}
return empMatch;
}
//HASH PRINT TABLE FUNCTION
void HashTable::printTable(){
for (int i{}; i< hashGroups ; i++){
if (table[i].size() == 0) continue;
auto tItr = table[i].begin();
for(;tItr !=table[i].end(); tItr++){
cout<<"info key : "<< tItr->first<<" value "<< tItr->second<<endl;
}
}
return;
}
//ADMIN MENU AND FUNCTIONS
void AdminMenu(){
int adminChoice=0;
int adminKey =0;
string adminValue;
HashTable HT;
cout<<"Please select from the following menu:\n";
cout<<"Option 1 insert user\n";
cout<<"Option 2 remove user\n";
cout<<"Option 3 Print user list\n";
cout<<"OPtion 4 Exit\n";
cin>>adminChoice;
switch(adminChoice){
case 1:
cout<<"Please enter employee id (whole number integer) to insert\n";
cin>>adminKey;
cout<<"Please enter password for employee id\n";
cin>>adminValue;
HT.insertItem(adminKey,adminValue);// DOESNT SEEM TO WORK
cout<<"insertion complete\n";
AdminMenu();
case 2:
cout<<"Please enter employee id (whole number integer) to remove\n";
cin>>adminKey;
HT.removeItem(adminKey); //DOESNT' SEEM TO WORK
cout<<"removal complete\n";
AdminMenu();
case 3:
HT.printTable(); //DOESN'T SEEM TO WORK.
AdminMenu();
case 4:
exit(0);
break;
}
}
//TODO implement a function that checks password against database or hashmap
void DisplayInfo(){
string clients[2][5] = {{"Bob Jones","Sarah Davis","Amy Friendly","Johnny Smith","Carol Spears"},{"2","2","1","1","2"}};
cout << "You chose 1\n" << "Client's Name Service Selected (1 = Brokerage, 2 = Retirement)\n";
for(int i=0;i<5;i++) {
cout << clients[0][i] << " selected option " << clients[1][i] << endl;
}
}
void changeCustomerChoice(){
string clients[2][5] = {{"Bob Jones","Sarah Davis","Amy Friendly","Johnny Smith","Carol Spears"},{"","2","1","1","2"}};
int temp= 0; //intialized at 0
cout << "You chose 2\n";
cout << "Enter the number of client you wish to change : \n";
cin >> temp; //this will check the type of input by the user.
while(1){ //this is an infinite check for the cin
if (cin.fail()){//this returns true when input failure occurs
cin.clear();// this clears the error state of the buffer
cin.ignore(numeric_limits<streamsize>::max(),'\n');//ignores the first instance of error
cout<<"You have entered the wrong input";
break;
}
if(temp>5||temp<1){
cin.clear();
cout<<"Selection out of range";
break;
}
if(!cin.fail()){// allow selection of services if input is correct
cout << "Please enter the client's new service choice (1 = Brokerage, 2 = Retirement)\n";
cin >> clients[1][temp-1]; //input needs to be adjusted to limit only 1 or 2 for change to new service
cout << "Changed option to : "<<clients[1][temp-1];
break;
}
}
}
int main()
{
HashTable HT;
int empNum= 0;
string empPass;
int userChoice;
int adminPass;
//insert values to i dont have a empty table
HT.insertItem(905,"Jim");
HT.insertItem(9415,"bob");
HT.insertItem(5105,"sany");
HT.insertItem(685,"Jasdfm");
HT.insertItem(9565,"amrk");
HT.insertItem(905,"trick");
HT.printTable();
cout<<"please enter employee number\n";
cout<<"please enter employee password\n";
cin>>empNum;
cin>>empPass;
HT.searchTable(empNum,empPass);
if(empMatch){
cout<<"access granted";
cout<<"What would you like to do?\n";
cout<<"DISPLAY the client list (enter1)\n";
cout<<"CHANGE a clients choice (enter2)\n";
cout<<"Admin functions (enter3)\n";
cout<<"Exit the program(enter4)\n";
cin>> userChoice;
switch(userChoice){
case 1:
DisplayInfo();
main();
break;
case 2:
changeCustomerChoice();
main();
break;
case 3:
cout<<"Please enter admin password\n";
cin>> adminPass;
if(adminPass == 5691){
AdminMenu();
break;
}
else{
break;
}
case 4:
exit(0); //exit program
default: //secures input from usering being malicious in nature
cout << "Invalid Selection" << endl;
cin.clear(); //clears the error state of the buffer
main(); // got back to main function
}
}
if(!empMatch){
cout<<"The creditionals you have entered do not match\n";
exit(0);
}
return 0;
`