如何解决嵌套 switch case 中的 C++ Hashtable 问题
所以我遇到了哈希表函数的问题。在主函数中,哈希表函数(removeitem、insertItem 和 printTable)可以正常工作,但是当我尝试在另一个函数 AdminMenu 中调用它们时,它们似乎不起作用。例如,当我运行程序时,我可以毫无问题地获得 AdminMenu,但是一旦在 Admin 菜单中选择一个选项会生成 cout 语句,但不会完成该功能。例如,当您选择 3 来打印表格时,除了数字 3 之外什么都没有输出。当该函数在 main 开始时被调用但在嵌套 switch case 中调用它时,该函数起作用,并且它不会执行 printTable 的函数,只是输出选择 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","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",{"","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;
`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。