Question
Lab06.zip We have recently been discussing the collections provided by the C++ standard library, such as vectors and maps . Recall that maps are collections
Lab06.zip
We have recently been discussing the collections provided by the C++ standard library, such as vectors and maps . Recall that maps are collections of values, but unlike vectors , the elements within the collection are stored as key/value pairs . This means that, instead of referring to an element according to its index (its location within the collection), as we do with vectors, we refer to the elements of a map by their keys (the names that we assigned to them when we added them to the collection). See the "Computation, Part 3" lecture notes to refresh your memory on maps.
This assignment is intended to give you some additional practice in working with maps, with user-defined data types, and with file input and output. Begin by downloading and extracting the attached archive to a folder; inside this folder, you will find a program file and an input file. You will write a simple program which opens the input file, which contains a "database" of names and addresses, each line corresponding to the information about a single person. Your program will read each person's information into an associative collection (a map), and will then add this collection to a bigger collection which will act as a container for every person's information. Finally, the program will output this information to the screen, and later to a text file, in the form of standard US address blocks (as you would see on printed envelopes and other mailed materials).
(The input file is an example of a character-separated values file, which is often used to store tables of information in text form. In this particular file, the "rows"also known as records correspond to information about a single person, and the "columns"also known as fields within each row correspond to individual attributes of the person. These fields are joined with commas, which act as delimiters for the fields. The information about each person includes their name and address, as well as a series of shipping rates. )
Begin by studying the partial implementation included with this exercise. Note that it begins by creating input and output streams ifstream and ofstream objects, respectivelycorresponding to the input and output files. Next, it creates a nested data structure in the form of a vector of maps , which will act as a "container" for all the information in the file. Each person's information is read into a new map , one field at a time, and then these maps are added to the vector . Before entering the main input loop, however, the program skips the first line of the file, which is a header row that documents the layout of the file. (These header rows are typically used only for documentation; they are not part of the data and thus should not be processed with it.)
When the main input loop is finished, the vector contains 500 maps, one for each person in the database. The input and output files are then closed. Notice that all of these operations take place inside a try block; if any unrecoverable errors are encountered along the way, such as a missing input file, the program will print the error information to the screen and exit. (We will revisit try and catch blocks shortly, when we study exceptions and error handling.)
Part One
Your first step is to complete this program by writing a function which iterates through the collections, retrieving each person's information from each map and writing this information to the screen. This function should accept the vector (" people ") and the file output stream object (" ost ") as arguments.
Notice that the last three fields on each line of the input file, " RateA ", " RateB ", and " RateC ", are a list of postage prices, but only one of them should be selected for output. To select the correct price, check the person's box type (this is in the map under a key called " box "). There are three types of standard regional rate boxes, identified as "A", "B", or "C". If the person's box type is "A", your program should choose the first price (" RateA "); if the person's box type is "B", it should choose the second price (" RateB "); and if the person's box type is "C", it should choose the third price (" RateC "). You might find it helpful to use a switch statement for this.
Here is an example of what the output should look like (only the first five people out of the 500 in the file are shown):
Martha C Zermeno 928 Arrowood Drive Jacksonville, FL 32217 $8.26 Erin F Smallwood 4474 Elk Street Los Angeles, CA 90017 $10.66 Dewayne J Brown 2403 Hurry Street Elkton, VA 22827 $47.43 Latrisha G Thornhill 1422 Oliver Street Fort Worth, TX 76118 $7.55 Larue J Villarreal 2907 Joy Lane Calabasas, CA 91302 $33.03
Remember that, to retrieve a value from a map, we must subscript the map with the name of the corresponding key , as in the following example: cout << person["lastname"] << ' '; // "person" is a map containing a key called "lastname" However, since our maps are also stored inside a vector, we must first select the correct map from the vector. We can do this by double subscripting the name of the vector, like so: cout << people[0]["firstname"] << ' ' << people[0]["middlename"] << ' ' << people[0]["lastname"] << ' '; (If you are doing this inside a for-loop, you should of course replace the zero inside the first pairs of brackets with the control variable for your loop.)
Part Two
Once your program is able to produce the output to the screen in the correct formatand do not move on to this step until it is!modify your program to write this data to the output file instead of cout ; to do this, replace every instance of cout in your function with ost (the output stream defined in the program and passed to your function as an argument).
Now, the data will be written to this output stream in the same way it was written to standard output using cout ; the only difference is the name of the output stream. After the program executes, view the contents of the output file in a text editor (such as Visual Studio Code) to check your work. Note that, if the output file does not exist, it will be created; if it already exists, its contents will be overwritten.
dev c++ please
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started