I could use some help with my code. I think one of my arrays is messed up becaus
ID: 3679551 • Letter: I
Question
I could use some help with my code. I think one of my arrays is messed up because I keep getting error messages I can't fix. :( How would you do this lab? THANK YOU!!
SUMMARY In this lab you are going to implement a simplified airline server that allows customers to register as frequent flyers and allows them to add flight mileage to their accounts. Your program will take two filenames as command line arguments: 1) a file that contains the names of flights and the miles flown on each flight, and 2) a file that contains commands to register customers as frequent flyers or to add flight mileage to their accounts. At the end of the program you will print a report that contains each frequent flyers’ name and the number of miles in their account. As an example, brad flew AA21 twice, for a total of 2046 miles and AA5138 once, for a total of 121 miles, and hence has a total of 2167 miles in his account. Your program will also check for a variety of potential errors and print a formatted error report containing any errors that have been detected.
PART 1 To create a customer report you will need 4 arrays. 2 arrays will keep track of the information from the flight file—a string array for flight names and an integer array for flight mileage. 2 arrays will keep track of frequent flyers and their accumulated flight mileage. The arrays that hold the information for flights should have a capacity of 20 and the arrays that hold the information for frequent flyers should have a capacity of 10 (of course in the real world these numbers would be much larger). Your program will create the customer report as follows:
1. Write a function named readFlights that reads the flight file into the flight name and flight mileage arrays. This function takes 4 parameters: a. The name of the flight file as a string b. The flight name array c. The flight mileage array d. The capacity of the two arrays (since these are parallel arrays, the capacity will be the same for each of them). readFlights returns the number of flights that are read. For this part you may assume that the number of flights is less than or equal to the arrays’ capacity.
2. Write a function named findIndex that finds and returns the index of a target string in a string array. For example, if findIndex were passed an array with the flights shown in the introduction and was asked to find the index of “AA42” (i.e., the target string), it would return 3. findIndex takes three parameters: a. A string array b. The number of elements in the string array (the size, not the capacity) c. The target string findIndex returns -1 if the target string is not found in the array. For example, if the target string is “AA1000” and the array contains the flights shown in the introduction, then findIndex returns -1.
3. Write the main function. It must: a. Call readFlights to fill the flight name and flight mileage arrays b. Read the command file one command at a time and perform one of the following actions: 1 register name: Add the customer’s name to the frequent flyers array and initialize the corresponding entry in the accumulated mileage array to 0. 2 record flyerName flightName: Call the findIndex function to locate the index of the flyer in the frequent flyers array and then call it a second time to locate the index of the flight in the flight name array. Once you have these two indices, you can use them to add the flight’s mileage to the flyer’s accumulated mileage. Notice how you are able to use this function to perform two different tasks. Also note that it is critical that you do not hard code the string array’s size into the function since the function needs to deal with arrays of different sizes. c. Print the frequent flyer report. Don’t format the report for this part. Instead print the flyer’s name, a space, and the flyer’s mileage. For example: "brad 2167/n" "mary 121". Don't preform any error checking yet.
PART 2 In this part you must add I/O manipulators to your cout statements to format the frequent flyer report so that the names and mileages end up in nicely aligned columns. You will format the report as follows: 1. The flyer’s name is left aligned in a field 15 characters wide 2. The flyer’s accumulated mileage is right aligned in a field 5 characters wide. Please dont put any spaces between the two fields.
PART 3 (last part) In the real world you need to perform error checking and in this part you will add the following error checks to your program:
1. The user must provide 2 command line arguments. If the number is incorrect, print the error message:"requires 2 filenames as command line arguments-a flight file and a command file."
2. The user must enter a valid file name for the flights file. If the user has entered an invalid flight file name, the function that opens the file will fail. Use the perror function to print the name of the invalid file name and the reason the open failed. Modify the function readFlights so that it returns 0 if this error is detected.
3. The user must enter a valid file name for the commands file. If the user has entered an invalid commands file name, the function that opens the file will fail. Use the perror function to print the name of the invalid file name and the reason the open failed.
4. The number of flights in the flights file must not exceed the capacity of the flight name array. If the number of flights exceeds the capacity of the flight name array, print the error message: "array capacity for flights has been exceeded" Modify the function readFlights so that it returns 0 if this error is detected
5. The number of flyers in the frequent flyers array must not exceed the capacity of the frequent flyers array. If a register command would cause this capacity to be exceeded, print the error message: "register flyerName Array capacity for flyers exceeded" The error message must be formatted as follows: a. “register” is left justified in a field 10 characters. b. flyerName is printed left justified in a field 15 characters wide. c. the error message is left justified. Don’t set the field width for the error message.
6. When a command registers a flyer, the flyer must not be in the frequent flyers array. If the flyer already exists, print the error message: "register flyerName This flyer is already registered". Follow the formatting instructions for the previous error message. You must use the findIndex function to determine if the flyer already exists.
7. When a command records a flyer’s mileage, the flyer must be in the frequent flyers array. If the flyer does not exist, print the error message: "record flyerName flightName This flyer is not registered" Format this message as follows: a. “record” is left justified in a field 10 characters wide b. flyerName is left justified in a field 15 characters wide c. flightName is left justified in a field 10 characters wide d. The error message is left justified. Do not set a field width for this message.
8. When a command records a flyer’s mileage, the flight must be in the flight names array. If the flight does not exist, print the error message: "record flyerName flightName This flyer is not registered" Use the same formatting instructions as for the previous error message.
YOU ARE DONE!!
Here is some additional information about the error checks:
1. The first four error checks will be performed at the beginning of the program. They will terminate the program if an error is discovered. If the error occurs in readFlights, then readFlights returns 0 and main will terminate the program if readFlights returns 0.
2. The remaining four error checks will be performed as commands are read. a. If the command tries to register a flyer whose addition will exceed the capacity of the frequent flyers array, the program will be terminated. b. In the remaining four cases, the program will discard the incorrect command and continue processing the remaining commands.
3. You do not have to check to see if the command is a valid command. If the command is invalid, we cannot be sure of how many fields we might need to discard. We could solve the problem by reading the command file a line at a time and using string streams to break the fields into lines. However we will give you practice with string streams in subsequent labs.
Explanation / Answer
ANSWER:
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int readFlights(string flightFile, string flightName[], int flightMileage[], int capactiy);
int findIndex(string array[], string size[], string target);
int main()
{
string commands, flights, flightName;
cout << "Frequent Flyer Report" << endl << endl;
ifstream target;
target.open("flights.txt");
if (target.fail()){
perror("flights.txt");
return -1;//error if not in the flights
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.