Hi I was wondering if someone could give me the code for this example problem I missed in class yesterday. My professor has not posted the notes yet because he got sick and I need this example problem code to study for my homework. If anyone has the code that works for this example please share it. Thanks!
This example, will create an object-oriented program with classes, to
1. read in records for all books available from an input file into C++ objects representing book
records,
2. sort those records alphabetically based on their title (more on that later),
3. write the sorted records into a different file.
Input/output specifications:
1. The input file will be called ‘bookinput.dat’ (without the quotes).
2. The output file will be called ‘bookoutput.dat’ (without the quotes).
3. Each line in the input file will contain entry for exactly one book, and the same needs to happen
for the output file.
4. Number of records in a file shall not be known upfront.
5. The book records will be made up of the following fields (with data types in parenthesis):
1. 6-digit book ID (string)
2. Author (string)
3. Title (string)
4. Edition (int)
5. Year published (int)
6. A 10-digit alphanumeric ISBN (International Standard Book Number) (string)
The data types must be as shown. See later in this file for an example input file.
Program Operation:
1. When run, the program will read the file bookinput.dat. If the file does not exist or is empty, it
will report that and then exit the program.
2. No book data will be input from the keyboard. All data shall be read from the input file.
3. You can safely assume the input file will contain no errors.
4. If the file exists and is not empty, the program shall sort all book records on their title, in
increasing lexicographic order.
5. No data shall be output to the screen. Every input/output operation will happen within the two
files.
To handle the tasks as explained above, you have to create a class to represent a “book” record. Each
book will thus become an object (remember: class==blueprint, object==actual instance). Specifically,
1. As you read each line from bookinput.dat, create an object of type Book
2. Store that Book object into an array, keeping a track of how many Book objects have been
added to the array. You can assume the maximum number of Books will be 2000.
3. Sort this array in increasing order by title.
4. Write each object of this array to the output file in the same format as the input file, of course,
in the same sorted order in increasing title.
Book Class Specification:
1. The class will be called “Book” (without the quotes);
2. All data members (variables) will be private;
3. Each data member thus will need one method to read from it, and one method to write to it.
Remember, these methods are called getters and setters, respectively.
4. The class also needs to have two constructors; one default, and the other with six arguments, to
set the values of the data members at once during construction.
5. Methods that only have read-only access to data members must be defined as const methods.
See example stub below.
6. All methods must be public.
To assist you to get started with this, a stub (that is, incomplete code) is provided on the next page for
the class declaration. It is missing important function declarations and other elements, which you have
to complete. The sorting function should be a non-member function of the Book class (hint: have
friends!).
Example Output (must be only in the file, do not show on the screen):
For the input shown here:
000001, Bjarne Stroustrup, The C++ Programming Language, 4, 2013, 0321563840
000002, Stephen Colbert, Stephen Colbert's Midnight Confessions, 1, 2017, 1501169009
000003, Tom Clancy, The Hunt for Red October, 2, 1984, 0425240339
000004, Gregory Dudek, Computational Principles of Mobile Robotics, 2, 2010, 0521692121
the output file would contain:
000004, Gregory Dudek, Computational Principles of Mobile Robotics, 2, 2010, 0521692121
000002, Stephen Colbert, Stephen Colbert's Midnight Confessions, 1, 2017, 1501169009
000001, Bjarne Stroustrup, The C++ Programming Language, 4, 2013, 0321563840
000003, Tom Clancy, The Hunt for Red October, 2, 1984, 0425240339
large examples usually fail to make their point.
but sorting a class is not too bad. Most of the time, you sort off just 1 field, here, the book title.
so your class
class book
{
stuff;
string title;
other stuff;
};
put that into an array
book books [1000];
and you can then just use the standard sort on it; it knows how to sort strings. It might be most wise to upcase the titles before sorting, though, you can add a private 'upcasetitle' field to the class and set it whenever title is assigned from anywhere.
save his sorting example code once he posts it, it might be handy later.
They don't have the code either because he hasn't posted his notes yet because he is sick. My friends took notes about the code but didn't think to take a picture because he was supposed to post the notes. This is not an assignment. I need this to study to do the homework. The homework assignment is about complex numbers, bubble sorts, and sorting arrays by size. This is similar but different. So if anyone could help me by showing me the code to the example I would appreciate it. I can show my friends so we can do the homework through the example problem.
It is too big to rewrite all of that just to have you get a copy of it in a bit when he posts it, and what we did may not even look at all like his design.
Another student from your class has part of the code posted in another thread, though.
What I advise is to start working on the homework cold. Do as much as you can, and when you get stuck, ask for help on that. Maybe post the actual homework questions so we can see those? You probably need a complex number class, that should be pretty straightforward to do to get started?