So here's the problem... my program is supposed to prompt the user to enter a number and use that value to determine what function the program should call via a switch block. Unfortunately, it seems that my program doesn't let the user enter an option and just keeps running the loop indefinitely. Here's the code:
#include <iostream>
#include <string>
#define TABLE_SIZE 25
#define DEBUG 1 // 0 = no debug output. 1 = display debug output
using namespace std;
struct user {
string user_name;
int pin;
};
typedef struct user User;
struct node {
User u;
struct node* next;
};
typedef struct node Node;
typedef Node* Nodeptr;
typedef unsigned int hval; // typedef for the hash value
void createNewUser(void);
hval getHashValue(User usr);
void insertNewUser(Node db[TABLE_SIZE], User usr);
void searchForUser(void);
void deleteUser(void);
Node dBase[TABLE_SIZE];
int main() {
// Main program loop
bool databaseIsEmpty = true;
bool run = true;
while (run) {
// Check to see if the database is empty
for (int i = 0; i < TABLE_SIZE; i++)
if (dBase[i].u.user_name != "") databaseIsEmpty = false;
// If the database is empty, require the user to fill out a new entry
if (databaseIsEmpty) {
std::cout << "The database is empty. Make a new entry!" << endl;
createNewUser();
}
string optstr = "Please enter an option. Your options are:\n"
" 1. Create a new user\n"
" 2. Search for a user and display his/her pin\n"
" 3. Delete a user\n"
" 4. Quit program\n"
"Enter your option: ";
int option = 0;
std::cout << optstr;
std::cin >> option;
switch (option) {
case 1:
createNewUser();
break;
case 2:
searchForUser();
break;
case 3:
deleteUser();
break;
case 4:
run = false;
break;
default:
std::cout << "You entered an invalid option value." << endl;
break;
}
}
std::cout << "Program works so far." << endl; // Shows that it gets to the return statement
return 0;
}
void createNewUser() {
string name;
int pin;
std::cout << "Enter user's name (case-sensitive): ";
std::cin >> name;
std::cout << endl << "Enter a PIN number for this person: ";
std::cin >> pin;
std::cout << endl;
User u;
u.user_name = name;
u.pin = pin;
insertNewUser(dBase, u);
}
hval getHashValue(User usr) {
hval hash;
size_t i;
size_t len;
string uname = usr.user_name;
len = uname.length();
for (hash = (hval)len, i = 0; i < len; i++) {
hash += uname[i];
}
return (hash % TABLE_SIZE);
}
void insertNewUser(Node db[TABLE_SIZE], User usr) {
// Check to see if there is already a user there.
// If no user is present, make that person the first user in the node.
// Otherwise, add them into the linked list.
hval h = getHashValue(usr);
if (db[h].u.user_name == "") {
// Empty user. Put them here.
db[h].u = usr;
} else {
Nodeptr p;
p = db[h].next;
while (p != NULL)
p = p->next;
Nodeptr q = new Node;
q->u = usr;
p->next = q;
}
}
void searchForUser() {
}
void deleteUser() {
}
// End
The last two functions aren't implemented yet, but I'm sure that's irrelevant. Any idea what could be going on here?
Thanks!!