Challenge 19

Description

The purpose of this challenge is load a struct array from a file. This challenge loads a file of airport codes and allows the user to query its contents.

 Requirements

  1. Using vi, create a text file that will contain the following data as shown below. Ask yourself, Why do the cities with multiple words use a dash instead of an actual space? (Los-Angeles instead of Los Angeles)
    BFL Bakersfield 3
    LAX Los-Angeles 35
    DFW Dallas 21
    IAH Houston 23
    ONT Ontario 14
    LGA La-Guardia 37
    AGU Aguascalientes 12
    HNL Honolulu 16
    JFK New-York 36
    
  2. Create a struct that will hold airport data
    struct Airport {
      string code;
      string city;
      int terminals;
    };
    
  3. Using a function int load_airports(Airport airports[]). This function will return the number of airport records (lines of text) actually read from the file.
  4. Write a function bool find_airport(Airport airports[], string code, Airport & match). This function will return true if code is matched with one of the airports in the airports array. It will return false otherwise. Additionally, if a matching airport is found in the array, then fill up the match parameter with the relevant information (it’s a struct, so make sure to fill/set all its properties). Notice that the match parameter is passed by reference. The find_airport() function should not do any cin or cout inside of it – its job is to search an array, that’s it.
  5. In main(), ask the user to enter an airport code. Call the find_airport() function to determine if the code entered by the user is in the array or airports. Display an appropriate message letting the user know if the code didn’t turn up a matching airport (See interaction). If the user enters the word “STOP” then stop asking for codes and exit the program.

Sample main()

int main()
{
  const int SIZE = 10;
  Airport airports[SIZE];
  int count;
  string code;  // used for searching

  count = load_airports(airports);

  // ask the user to enter a code repeatedly until the user
  // enters STOP as the airport code

  return 0;
}

Sample Interaction

Enter an airport code: BFL
BFL (Bakersfield) has 3 terminals

Enter an airport code: HNL
HNL (Honolulu) has 16 terminals

Enter an airport code: YUV
No matching record found for YUV

Enter an airport code: IAH
IAH (Houston) has 23 terminals

Enter an airport code: STOP

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0019

Print Requirements