Lab 9: Travel Agent

This assignment assumes that you have read up through Chapter 11 (File I/O). Please bring your book to lab, as you will need it as a reference. Each lab assignment is more work than the previous one, so get started early.

This project may be done in pairs of students. We suggest that you sit together at the same computer and apply the pair programming technique. Take turns as the driver and navigator. (If you prefer to work alone, that is ok too.)

The objectives for this assignment are for you to:

  • Demonstrate that you can read from and write to files.
  • Gain more experience in handling strings and arrays.
  • Get even more practice in decomposing programs into small functions.
  • Getting Started

    As in the previous lab, start by creating a directory just for this assignment, and changing to that directory:
    mkdir cse20211/lab9
    cd cse20211/lab9
    

    Travel Agent

    Write a program (travel.c) to assist the user in booking a flight with multiple hops. The program will load a list of flights from a file (flights.txt) ask the user for the starting airport, then display the available flights. For each hop, the user picks the next flight, and repeats until done. When complete, the program will write out the complete itinerary to a file called itinerary.txt.

    A master list of all flights is listed in a file called flights.txt, which you should download and put in your lab directory. (This is fictional data I generated with a program, but you can download and play with real data from the Open Flights project.)

    The travel agent program should begin by loading the entire list of flights into several arrays in memory. You will use these arrays throughout the program to display the available flights and decide what to do next.

    Here are a few lines from the middle of the file:

    ..
    SBN MSP 15:16 17:22 $229
    SBN MSP 17:21 20:44 $137
    SBN DEN 18:44 21:30 $213
    ORD ATL 6:31 9:56 $373
    ORD DTW 7:50 9:01 $205
    ORD BOS 8:44 9:56 $105
    ..
    
    Each line in the file describes the origin airport, the destination airport, the time of arrival and departure (on a 24-hour clock) and the cost of the flight. For example, the first line above indicates a flight departing South Bend at 15:16, arriving at Minneapolis on 17:22, and costing $229.

    Your program should begin by asking the user what airport they are starting at. Then, display all the flights available for that day from that airport, allowing the user to pick one. Once a flight is selected, display the flights available from that airport. The user can type end to indicate the itinerary is complete, or cancel to cancel the previous flight.

    When the itinerary is complete, open a file named itinerary.txt and write out a nicely formatted itinerary with the name of your travel agency, the list of the flights (arranged in labelled columns), and the detailed and total costs.

    Hints to keep in mind:

  • Create a struct to represent a single flight, then create an array of structures to represent all of the flights.

  • To keep track of the itinerary select so far, just use a simple integer array named itinerary, which contains each of the flight numbers selected so far. For example, an itinerary of {0,30,94} indicates SBN-MSP-IND-DCA.

  • Don't forget time! If a flight arrives at an airport at noon, then the user should only be able to pick outgiong afternoon flights. The entire itinerary must complete within the same day.

  • The flight data in the file is kept in a very 'raw' form. When you display flight choices and print out the final itinerary, format and explain the data so that it is easy to read and understand.

  • Divide your program into simple, obviously named functions such as print_flight(i) and find_flights(origin,time)

  • To keep the program simple, you may assume that there are no more than 100 flights in the file, no more than 100 airports, and no more than 100 flights in an itinerary. However, keep in mind that the flight database could change (perhaps daily), so don't make any assumption about airport names, flight times, and so forth.

  • Be careful to check for a variety of errors, and accomodate them gracefully. The user could enter an airport that doesn't exist, a flight that doesn't make sense, and so on....
  • Makefiles

    Are you sick of typing in gcc over and over again? A Makefile is the universal way of automating compilations for C programs, and many other languages and tasks as well. To use Make for this project, create a file called Makefile with the following contents:
    travel: travel.c
            gcc travel.c -o travel
    
    clean:
            rm -f travel
    
    (Note that you must have a TAB at the beginning of the line that starts with gcc. If you put in spaces, it won't work.)

    Each rule in a Makefile indicates how to build a part of your program. The first line of the Makefile indicates that travel depends on the source file travel.c. The second line of the Makefile indicates how to build travel, by giving the compile instruction. It is also customary to have a clean rule that explains how to remove whatever the Makefile created.

    Once the Makefile exists, all you have to do is type make, which examines the Makefile and decides what to do. For very complex programs, a Makefile is an indispensable tool for managing the build process.

    For this lab, you are required to use and turn in a Makefile.

    Turning In

    Please review the general instructions for lab reports.

    Turn in travel.c, Makefile, and report.txt. Your lab report should cover the assignment, explaning how it works from the user perspective, how the program works internally, and how you verified that the output of the program is correct.

    If you are working in pairs, turn in your assignment to either student's dropbox (not both) and clearly indicate the names of both students in your report. Both students will receive the same grade on the project.

    This assignment is due on Monday, November 19th at noon Tuesday, November 19th at 5pm. Late assignments are not accepted.