kinda like the last program.. just does different things..

anyway, im getting a seg fault i dont understand one bit
its off of the menu function.

when you input an invalid number like 8 - it says invalid
but if you input 7 to quit the program it seg faults.

also, if you run one of the other menu options... it will go into that
function and it will ask the desired question but when you type
a value in it will seg fault.

is my input incorrect?
thanks.

/*This program will work as a database for storing employee 
information for a company.  It will work as a binary tree.
The company information will be read in as a data file:
    
    proj4.dat

The data will then be available for search.  After the search
is completed, the information will be printed to the screen
and also will be saved into a data file:
    
    results.dat

*/

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

class binaryTree {

	public:
        // constructor, destructor, and menu functions   
		binaryTree ();
		~binaryTree ();
		void menu ();

	private:
        // struct used to gather information for a data file    
		struct info {
       
               string employee;
               string lastName;
               string firstName;
               char middleInitial;
               string state;
               string zipcode;
               string department;
               int yearsWorked;
               int salary;
               struct info * right; //right pointer
               struct info * left;  // left pointer
        };
	
		info * root; // base of the tree
		
        // functions used in the program
		void information (info &);
		void makeDataFile (info &);
        void insert (info *, info &);
		void retrieveEmployee (info *);
		void retrieveLast (info *);
		void retrieveFirst (info *);
		void retrieveState (info *);
		void retrieveDepartment (info *);
		void retrieveZip (info *);
		
};

binaryTree::binaryTree() {
        // constructor to declare pointers used as null
        
        info * root = NULL;
        info * right = NULL;
        info * left = NULL;
        
}

binaryTree::~binaryTree() {
               // simple destructor that doesnt do much
               // didnt know what to put here since we 
               // didnt do a delete function                  
        delete root;
        
}

void binaryTree::information(info & person) {
        // this function is used for input 
        // person is a call by reference to
        // the struct is used here to store
        // the information that is necessary                  
        // the inputs are done from a data file
        // named proj4.dat
    ifstream file; // creates instance of ifstream
    file.open ("proj4.dat"); 
    
    file >> person.employee;
    file >> person.lastName;
    file >> person.firstName;
    file >> person.middleInitial;
    file >> person.state;
    file >> person.zipcode;
    file >> person.department;
    file >> person.yearsWorked;
    file >> person.salary;
    
    file.close();
       
}

void binaryTree::makeDataFile (info & person) {
         // this function is used to make a data
         // file from the output generated when a
         // search took place.  person is call by
         // reference and is used to bring in the
         // data to place in output.
         // the results can be found in results.dat
     ofstream output; // creates instance of ofstream
     output.open("results.dat");
     
     output << person.employee;
     output << person.lastName;
     output << person.firstName;
     output << person.middleInitial;
     output << person.state;
     output << person.zipcode;
     output << person.department;
     output << person.yearsWorked;
     output << person.salary;
     
     output.close();
     
}

void binaryTree::insert(info * tree, info & person) {
    // this will create the tree and insert the information into
    // the tree as needed.  a pointer of struct info type referred
    // to as tree is call by value.  the person parameter is call by
    // reference and brings in the information you have inputed and 
    // places it into the tree 
    if (tree == NULL) {
 
        tree = new info;
        tree -> left = NULL;
		tree -> right = NULL;
		tree -> employee = person.employee; 
		tree -> lastName = person.lastName;
        tree -> firstName = person.firstName;
        tree -> middleInitial = person.middleInitial;
        tree -> state = person.state;
        tree -> zipcode = person.zipcode;
        tree -> department = person.department;
        tree -> yearsWorked = person.yearsWorked;
        tree -> salary = person.salary;
        
	}
}

void binaryTree::retrieveEmployee(info * tree) {
     // this function will print out all the information
     // for the employee that is inputed by the user
     // a pointer of struct info type referred
     // to as tree is call by value.
     
     // The search function is for employee number
     string input;     
     cout << "Please input the ID number you wish to search for: ";
     cin >> input;  
     while (root != NULL) {                        
         if (input == tree -> employee) {
                   retrieveEmployee (tree -> left); //searches left child
                   cout << tree -> employee;
                   cout << tree -> lastName;
                   cout << tree -> firstName;
                   cout << tree -> middleInitial;
                   cout << tree -> state;
                   cout << tree -> zipcode;
                   cout << tree -> department;
                   cout << tree -> yearsWorked;
                   cout << tree -> salary;
                   retrieveEmployee (tree -> right); // searches right child
         }
         else {         
                   cout << "There is no information that meets search critera";
                   cout << endl;
                   break;
         }
     }
     info person; // creates an instance of info called person
     makeDataFile(person); // calls makeDataFile function
}

void binaryTree::retrieveLast (info * tree) {
     // this function will print out all the information
     // for the employee that is inputed by the user
     // a pointer of struct info type referred
     // to as tree is call by value.
     
     // The search function is for last name
     string input; 
     cout << "Please input the last name you wish to search for: ";
     cin >> input;
     while (root != NULL) {                        
         if (input == tree -> lastName) {
                   retrieveEmployee (tree -> left); //searches left child
                   cout << tree -> employee;
                   cout << tree -> lastName;
                   cout << tree -> firstName;
                   cout << tree -> middleInitial;
                   cout << tree -> state;
                   cout << tree -> zipcode;
                   cout << tree -> department;
                   cout << tree -> yearsWorked;
                   cout << tree -> salary;
                   retrieveEmployee (tree -> right); // searches right child
         }
         else {         
                   cout << "There is no information that meets search critera";
                   cout << endl;
                   break;
         }
     }
     info person; // creates an instance of info called person
     makeDataFile(person); // calls makeDataFile function
}

void binaryTree::retrieveFirst (info * tree) {
     // this function will print out all the information
     // for the employee that is inputed by the user
     // a pointer of struct info type referred
     // to as tree is call by value.
     
     // The search function is for first name
     string input;
     cout << "Please input the first name you wish to search for: ";
     cin >> input;
     while (root != NULL) {                        
         if (input == tree -> firstName) {
                   retrieveEmployee (tree -> left); //searches left child
                   cout << tree -> employee;
                   cout << tree -> lastName;
                   cout << tree -> firstName;
                   cout << tree -> middleInitial;
                   cout << tree -> state;
                   cout << tree -> zipcode;
                   cout << tree -> department;
                   cout << tree -> yearsWorked;
                   cout << tree -> salary;
                   retrieveEmployee (tree -> right); // searches right child
         }
         else {         
                   cout << "There is no information that meets search critera";
                   cout << endl;
                   break;
         }
     }
     info person; // creates an instance of info called person
     makeDataFile(person); // calls makeDataFile function
}     

void binaryTree::retrieveState (info * tree) {
     // this function will print out all the information
     // for the employee that is inputed by the user
     // a pointer of struct info type referred
     // to as tree is call by value.
     
     // The search function is for state
     string input;
     cout << "Please input the state you wish to search for: ";
     cin >> input;
     while (root != NULL) {                        
         if (input == tree -> state) {
                   retrieveEmployee (tree -> left); //searches left child
                   cout << tree -> employee;
                   cout << tree -> lastName;
                   cout << tree -> firstName;
                   cout << tree -> middleInitial;
                   cout << tree -> state;
                   cout << tree -> zipcode;
                   cout << tree -> department;
                   cout << tree -> yearsWorked;
                   cout << tree -> salary;
                   retrieveEmployee (tree -> right); // searches right child
         }
         else {         
                   cout << "There is no information that meets search critera";
                   cout << endl;
                   break;
         }
     }
     info person; // creates an instance of info called person
     makeDataFile(person); // calls makeDataFile function
}     

void binaryTree::retrieveDepartment (info * tree) {
     // this function will print out all the information
     // for the employee that is inputed by the user
     // a pointer of struct info type referred
     // to as tree is call by value.
     
     // The search function is for department
     string input;
     cout << "Please input the department you wish to search for: ";
     cin >> input;
     while (root != NULL) {                        
         if (input == tree -> department) {
                   retrieveEmployee (tree -> left); //searches left child
                   cout << tree -> employee;
                   cout << tree -> lastName;
                   cout << tree -> firstName;
                   cout << tree -> middleInitial;
                   cout << tree -> state;
                   cout << tree -> zipcode;
                   cout << tree -> department;
                   cout << tree -> yearsWorked;
                   cout << tree -> salary;
                   retrieveEmployee (tree -> right); // searches right child
         }
         else {         
                   cout << "There is no information that meets search critera";
                   cout << endl;
                   break;
         }
     }
     info person; // creates an instance of info called person
     makeDataFile(person); // calls makeDataFile function
}     

void binaryTree::retrieveZip (info * tree) {
     // this function will print out all the information
     // for the employee that is inputed by the user
     // a pointer of struct info type referred
     // to as tree is call by value.
     
     // The search function is for zipcode
     string input;
     cout << "Please input the zipcode you wish to search for: ";
     cin >> input;
     while (root != NULL) {                        
         if (input == tree -> zipcode) {
                   retrieveEmployee (tree -> left); //searches left child
                   cout << tree -> employee;
                   cout << tree -> lastName;
                   cout << tree -> firstName;
                   cout << tree -> middleInitial;
                   cout << tree -> state;
                   cout << tree -> zipcode;
                   cout << tree -> department;
                   cout << tree -> yearsWorked;
                   cout << tree -> salary;
                   retrieveEmployee (tree -> right); // searches right child
         }
         else {         
                   cout << "There is no information that meets search critera";
                   cout << endl;
                   break;
         }
     }
     info person; // creates an instance of info called person
     makeDataFile(person); // calls makeDataFile function    
}     

void binaryTree::menu() {
     // the menu function will print out a menu and let the user
     // pick the option of choice.  it will then call the function 
     // that is requested.  it is set up to handle invalid entries
     // no data parameters are used for this function
     int choice = 0;
     info person; // declares instance of info called person
     information(person); // calls information function
     insert(root, person); // fills tree with the information 
     
     while (choice != 7) { // ends program if choice equals 7
         cout << endl;
         cout << "Welcome to the business. " << endl;
         cout << "Please choose your desired action. " << endl;
         cout << "1. Search for a specific employee ID. " << endl;
         cout << "2. Search by last name. " << endl;
         cout << "3. Search by first name. " << endl;
         cout << "4. Search by state. " << endl;
         cout << "5. Search by department. " << endl;
         cout << "6. Search by zip code. " << endl;
         cout << "7. Exit the program. " << endl;
         cin >> choice;
         cout << endl;
         
         if (choice == 1) { // displays information if user inputed 
                            // employee number matches one on the list
                    info tree; // creates instance of info called tree
                    retrieveEmployee(root);                
         }
         else if (choice == 2) { // displays information if user inputed 
                                // last name matches one on the list
                    info tree; // creates instance of info called tree
                    retrieveLast(root);                
         }
         else if (choice == 3) { // displays information if user inputed 
                                // first name matches one on the list
                    info tree; // creates instance of info called tree
                    retrieveFirst(root);                
         }
         else if (choice == 4) { // displays information if user inputed 
                                // state matches one on the list
                    info tree; // creates instance of info called tree
                    retrieveState(root);                
         }
         else if (choice == 5) { // displays information if user inputed 
                                // department matches one on the list
                    info tree; // creates instance of info called tree
                    retrieveDepartment(root);                
         }
         else if (choice == 6) { // displays information if user inputed 
                                // zip code matches one on the list
                    info tree; // creates instance of info called tree
                    retrieveZip(root);                
         }
         else if (choice == 7)  // terminates the while loop causing
                               // the program to end
                    break;                
         else 
              // error statement 
              // takes you back to menu
              cout << "Invalid option. Please retry." << endl;
     }
}
     
int main () {
    
    binaryTree binTree; // declares an instance of the class binaryTree
    binTree.menu(); // calls the menu function
    
    binTree.~binaryTree(); // calls the destructor on close

    return 0;
    
}

i still got a seg fault but it was good enough to turn it in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.