Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives to write this c program Creation and use of arrays Creation and use of structs Passing arguments into parameters Splitting your code into modular

Objectives to write this c program

Creation and use of arrays Creation and use of structs Passing arguments into parameters Splitting your code into modular functions Making appropriate design decisions Using best practices for magic numbers Command-line arguments File I/O, binary and text Following stated requirements Overview of Requirements

Use arrays to write a program to tell you how long it will take you to fly between any two cities on an extremely unusual airline. The times are:

Home City Destination City

Flying Time

Layover Time

(1) Toronto

(2) Atlanta

4:15

1:20

(2) Atlanta

(3) Austin

3:58

0:46

(3) Austin

(4) Denver

3:55

11:29

(4) Denver

(5) Kansas City

2:14

0:53

(5) Kansas City

(6) New York

3:27

none

Functional Requirements

The user must provide the starting and ending cities as a number. The numbers corresponding to each city are shown in the table above (e.g. Toronto is city 1, Kansas City is city 5). Do not change the numbers for the cities. Your program must loop until the user selects city 0 for the starting city. In the loop, display the city list (with city number and city name on the same line, one line per city) and then ask the user for the starting city and then for the ending city. Once you get the ending city, calculate and display the elapsed time to travel between the cities if they are valid OR display an error message if either of the cities are not valid. The result must be calculated. It must then be displayed in correct hh:mm format, not hours only or minutes only. The result should include all layovers between cities but not the layover once you get into the ending city. If the number of hours is less than 10, you must not have a leading 0 (i.e. "9:43" is OK, "09:43" is not). If the number of minutes is less than 10, you must have a leading 0 (i.e. "9:03" is OK, "9:3" is not). If the cities are valid, the output line must be exactly of the format "[city1name] to [city2name] will take [hh:mm]." Make sure that this is exact, even down to the punctuation and spacing. The error message is described below. This output line must also be written to a text file called "results.txt", stored in the current directory. Only these specific lines should be written to the file. I'm serious about not changing the city numbers. Do not have extra user input (including "Press any key to continue") beyond getting the starting and ending cities repeatedly. I am very serious about this. Don't clear the screen when doing output. Spelling and consistency in your output is important. Also, make good use of blank lines to separate unrelated parts of your output. The program must not produce incorrect output or crash or hang if invalid input is used. You must use either cin or the getNum() function from Assignment 2 for getting input. You are not responsible for handling excessively long input lines or numbers out of the range of an int variable. Programming Requirements

It must use at least one non-trivial (i.e. more than one or two lines long) user-created function other than main() and the function you use for getting user input. Parameters must be passed and/or return values used. It must not use global variables or goto. Any use of scanf() will be handled as described in class. The city names and flying and layover times for a city must be stored in a struct called CityInfo. All of this data must be stored as an array of structs stored in main(). This array must be passed to a function that does the time calculations (so that I can see that you know how to do it). The addition of the times must be done using a loop of some type. The flying and layover times must be loaded in from a binary file. Each time is a pair of two bytes. The first byte is the hours portion. The second byte is the minutes portion. For example, the time "4:15" is stored as one byte with the ASCII value 4 and a second byte with the ASCII value 15. It is your job to figure out how to convert this to minutes (which is easiest to work with). The name of the flying and layover times file must be obtained as the first command-line argument. For your internal testing, I will provide this binary file for you. I will test using this provided file but will use whatever filename I want. Loading this binary file into a text editor likely won't work very well for you. The city names must be loaded in from a text file. Each city name is stored in the file as a string on one line (so there should be 6 lines in the file). The name of the city name file must be obtained as the second command-line argument. You must create this file yourself and submit it, although I will use my own version. The city names must be read in from the file as C-style null-terminated strings using fgets() but they can be stored in the struct as either a C-style null-terminated string or a C++ string. The "results.txt" filename must be stored as a const char * variable called resultsFilename (i.e. const char *resultsFilename = "results.txt";) that you would then use in your fopen() call and error messages. This constant can be global. Do not hardcode the "results.txt" filename in your fopen() call. It would also be a good idea to not hardcode the "results.txt" filename in your error messages but I won't be checking that when marking. Use best practices with respect to Magic Numbers. Assume that the user will always enter less than 80 characters. The program must be commented adequately and indented correctly. Errors

If there is not exactly two command-line arguments, display an error message that states exactly "Usage: cA3 " and quits the program immediately. Do not support going backwards (e.g. from Denver to Toronto). If they try to do so, display an error message that states exactly "Error: flights cannot go backwards" and continue with the program. Do not support going from one city to the same city (e.g. from Denver to Denver). If they try to do so, display an error message that states exactly "Error: flights must leave the city" and continue with the program. If the user enters any input other than "1" to "6" and "0" (to exit), display an error message that states exactly "Error: invalid city" and continue with the program. If the invalid city is the ending city, assume that they also want to re-enter the starting city number. If they enter "0" as the ending city, this is an "invalid city" error and not a desire to quit the program. Error checking is required for all file I/O calls. Upon any file I/O errors, display the following specified error message exactly and quit the program: If any file is not openable: "Error opening [filename]", where [filename] is replaced by the actual filename of the file. If there are errors reading from a file: "Error reading [filename]", where [filename] is replaced by the actual filename of the file. Keep in mind that reading past end-of-file is not considered to be an error in this case. If there are errors writing to a file: "Error writing [filename]", where [filename] is replaced by the actual filename of the file. If there are errors closing a file: "Error closing [filename]", where [filename] is replaced by the actual filename of the file. NOTES: It is vitally important that you follow the above requirements. The user should never has to guess about what the city numbers are. Be aware that Visual Studio will NOT allow the use of const-declared values as array sizes. You must use #define instead if you want to avoid using a magic number in the size of an array. Note the change of city names from the original Assignment #4. This is designed to see if you can read multi-word city names into a string. In your internal testing, be aware that you must put your filenames in double-quotes if there are any spaces in the filenames or the paths. You will want to find out what Visual Studio considers "the current directory" for your testing. You will want to plan your design so that you can easily quit the program upon fatal errors. Using functions with return values is one way that you can do this (but not the only way). Modularity is your friend. A "layover" is an amount of time that you have to wait in the airport between flights. Make sure that you don't include the layover at the end of the trip! As an example, your program should be able to calculate how long it takes to fly from Austin to Kansas City or Toronto to Denver but not Kansas City to Austin (that's backwards). To fly from Austin to Kansas City , it would take 3:55 (Austin to Denver) + 11:29 (layover in Denver) + 2:14 (Denver to Kansas City). You do not include the Kansas City layover.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago