jhdobbins 0 Junior Poster

for example when i now search for a specific city and none match the function will tell you but if there is a match the program just dies, also when i try the retrieve all function, the program dies by seg fault... so im thinking its in my while loop?

else if (searchChoice == 2) { 
                dataNode *tree; 
                retrieveCity(root); 
           }
void binaryTree::retrieveCity(dataNode * tree) { 
      
     char city[30]; 
     cout << "What city would you like to see departing flights from? "; 
     cin.ignore(); 
     cin.getline(city, 30); 
      
     while (root!= NULL) { 
           if (tree -> departCity == city) { 
                        retrieveCity (tree -> left); 
                        cout << tree -> flightNumber << " " ; 
                        cout << tree -> departCity[30] << " "; 
                        cout << tree -> arriveCity[30] << " "; 
                        cout << tree -> passengers << " "; 
                        cout << tree -> totalPassengers << " " << endl; 
                        retrieveCity (tree -> right); 
           } 
           else { 
                        cout << "No cities match your current search." << endl; 
                        break; 
           } 
    } 
}

another thing i just found...

its skipping my if statements or something..

#include <iostream> 
#include <cstdlib> 

using namespace std; 

class binaryTree { 

   public: 

      binaryTree (); 
      ~binaryTree (); 
      void menu (); 

   private: 

      typedef struct dataNode { 
         char arriveCity[30]; 
         char departCity[30]; 
         int totalPassengers; 
         int passengers; 
         int flightNumber; 
         struct dataNode * left; 
         struct dataNode * right; 
      }; 
    
      dataNode * root; 
      void info (dataNode & newNode); 
        void insert (dataNode *, dataNode &); 
      void retrieveAll (dataNode *); 
      void retrieveOpen (dataNode *); 
      void retrieveCity (dataNode *); 

}; 

binaryTree::binaryTree() { 

   dataNode * root = NULL; 
   dataNode * left = NULL; 
   dataNode * right = NULL; 

} 

binaryTree::~binaryTree() { 
                          
   delete root; 
    
} 

void binaryTree::info(dataNode & newNode) { 
      
        cout << "Please input the six digit flight number (100000 - 999999): "; 
        cin >> newNode.flightNumber; 

        if (newNode.flightNumber < 100000 || newNode.flightNumber > 999999) 
            cout << "Invalid flight number reference" << endl << endl; 
        
        cin.ignore();    
        cout << endl << "What is the departure city? "; 
        cin.getline(newNode.departCity, 30); 
        cout << endl << "What city is the flight destination? "; 
        cin.getline(newNode.arriveCity, 30); 
        cout << endl << "What is the maximum capacity for this flight? "; 
        cin >> newNode.totalPassengers; 
        cout << endl << "How many passengers currently have tickets? "; 
        cin >> newNode.passengers; 
        cout << endl; 
} 

void binaryTree::insert(dataNode* tree, dataNode &newNode) { 
    
   if (tree == NULL) { 
  
        tree = new dataNode; 
        tree -> left = NULL; 
      tree -> right = NULL; 
      tree -> flightNumber = newNode.flightNumber; 
      tree -> totalPassengers = newNode.totalPassengers; 
        tree -> passengers = newNode.passengers; 
        strcpy(tree -> arriveCity, newNode.arriveCity); 
        strcpy(tree -> departCity, newNode.departCity); 
        
   } 
} 

void binaryTree::retrieveAll (dataNode * tree) { 

     while (root != NULL){  
           retrieveAll (tree -> left); 
           cout << tree -> flightNumber << " " ; 
           cout << tree -> departCity[30] << " "; 
           cout << tree -> arriveCity[30] << " "; 
           cout << tree -> passengers << " "; 
           cout << tree -> totalPassengers << " " << endl; 
           retrieveAll (tree -> right); 
      } 
} 

void binaryTree::retrieveOpen(dataNode * tree) {    
      
      while (root != NULL) { 
            if (tree -> passengers < tree -> totalPassengers) { 
                         retrieveOpen (tree -> left); 
                         cout << tree -> flightNumber << " " ; 
                         cout << tree -> departCity[30] << " "; 
                         cout << tree -> arriveCity[30] << " "; 
                         cout << tree -> passengers << " "; 
                         cout << tree -> totalPassengers << " " << endl; 
                         retrieveOpen (tree -> right); 
            } 
            else { 
                         cout << "There are currently no open flights." << endl; 
                         break; 
            } 
       } 
} 

void binaryTree::retrieveCity(dataNode * tree) { 
      
     char city[30]; 
     cout << "What city would you like to see departing flights from? "; 
     cin.ignore(); 
     cin.getline(city, 30); 
      
     while (root!= NULL) { 
           if (tree -> departCity == city) { 
                        retrieveCity (tree -> left); 
                        cout << tree -> flightNumber << " " ; 
                        cout << tree -> departCity[30] << " "; 
                        cout << tree -> arriveCity[30] << " "; 
                        cout << tree -> passengers << " "; 
                        cout << tree -> totalPassengers << " " << endl; 
                        retrieveCity (tree -> right); 
           } 
           else { 
                        cout << "No cities match your current search." << endl; 
                        break; 
           } 
    } 
}          
                                  
void binaryTree::menu () { 
    
    int choice = 0, searchChoice = 0; 
    
    while (choice != 3) {    
          
        cout << endl; 
        cout << "Welcome to the JHD International Airport! How can we be of assistance?" << endl; 
        cout << "Please select the best option for you:" << endl; 
        cout << "  1. Create a new flight record." << endl; 
        cout << "  2. Search for a flight." << endl; 
        cout << "  3. End current program session." << endl; 
        cin >> choice; 
        cout << endl; 
        
        if (choice == 1) { 
           dataNode newNode; 
           info(newNode); 
           insert(root, newNode); 
        } 
        else if (choice == 2) { 
    
          cout << "Please select the search option of your choice:" << endl; 
          cout << "  1. Display all flight records." << endl; 
          cout << "  2. Display all departing flights from a city." << endl; 
          cout << "  3. Display all open flights." << endl; 
          cout << "  4. Go back to the main menu." << endl; 
          cin >> searchChoice; 
          cout << endl; 
    
           if (searchChoice == 1) { 
                dataNode *tree; 
                retrieveAll(root); 
           } 
           else if (searchChoice == 2) { 
                dataNode *tree; 
                retrieveCity(root); 
           } 
           else if (searchChoice == 3) { 
                dataNode *tree; 
                retrieveOpen(root); 
           } 
        } 
        
        else if (choice == 3) 
             break; 
              
        else 
             cout << "That is an invalid option" << endl; 
    } 
} 

int main () { 

        binaryTree binTree; 
        binTree.menu(); 

return 0; 
}

and this is the output i am receiving..
ill put it into code tags for easy reading...

Welcome to the JHD International Airport! How can we be of assistance? 
Please select the best option for you: 
  1. Create a new flight record. 
  2. Search for a flight. 
  3. End current program session. 
1 

Please input the six digit flight number (100000 - 999999): 243245 

What is the departure city? bob 

What city is the flight destination? non 

What is the maximum capacity for this flight? 343 

How many passengers currently have tickets? 343 


Welcome to the JHD International Airport! How can we be of assistance? 
Please select the best option for you: 
  1. Create a new flight record. 
  2. Search for a flight. 
  3. End current program session. 
2 

Please select the search option of your choice: 
  1. Display all flight records. 
  2. Display all departing flights from a city. 
  3. Display all open flights. 
  4. Go back to the main menu. 
3 

There are currently no open flights. 

Welcome to the JHD International Airport! How can we be of assistance? 
Please select the best option for you: 
  1. Create a new flight record. 
  2. Search for a flight. 
  3. End current program session. 
2 

Please select the search option of your choice: 
  1. Display all flight records. 
  2. Display all departing flights from a city. 
  3. Display all open flights. 
  4. Go back to the main menu. 
2 

What city would you like to see departing flights from? bob 
No cities match your current search. 

Welcome to the JHD International Airport! How can we be of assistance? 
Please select the best option for you: 
  1. Create a new flight record. 
  2. Search for a flight. 
  3. End current program session.
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.