Object oriented Programming

In class, this example was used but I missed the program my professor showed. Could anyone help me with me what the program is? Any help would be appreciated. Thank you!

You 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 an 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)

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.
6. All methods must be public.

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
Sometimes you have to read the assignment multiple times to get a good sense of what's required. Here are some pointers:
- The output file and the input file are in the same format. The example output at the end shows what the format is.
- Use the "Book Class Specification" and item 5 from Input/Output to guide you in defining the Book Class. I'd start by writing that class.
- Next write a program that reads one instance of class Book from the input file and prints it out.
- When that works, modify the program to read the whole input file and print the records to the output. Don't worry about sorting them yet. You're just trying to get it to read and write the data reliably.
- Finally, add the step to sort the data.

Work on this. When you get stuck for 30 minutes, post whatever code you have and describe the problem you're having.
I got this far and now I am not sure what to do and nothing happens with it.
I think I need a while statement to read the file but I am not sure what. I am also not sure what to add after it reads the file. Does the data have the file or am I putting the data into it?

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

class Book;
{
private:
string book_id;
string author;
string title;
int edition;
int year;
string ISBN;

public:
Book();
Book(string book_id, string author, string title, int edition, int year, string ISBN);

string getBook_id() const;
void setBook_id(string book_id);

string getAuthor() const;
void setAuthor(string author);

string getTitle() const;
void setTitle(string title):

int getEdition() const;
void setEdition(int edition);

int getYear() const;
void getYear(int year);

string getISBN() const;
void setISBN(string ISBN);

void sort(Book list[], int sine);
};

int main()
{
const char c= ',';
Book list[2000];
ifstream readFile;
ofstream writeFile;

int size=6, i=0, edition, year;
string book_id, author, title, ISBN, st;

readFile.open("bookinput.dat");

if(readFile.fail())
{
cout << "Cannot open file" << endl;
exit(1);
}

//while?

readFile.close();
writeFile.close();
return 0;
}
I got this far and now I am not sure what to do and nothing happens with it.
I think I need a while statement to read the file but I am not sure what. I am also not sure what to add after it reads the file. Does the data have the file or am I putting the data into it?

--------
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.

--The file clearly should have the data in it.
if you have already checked empty & exists and failed, then a do-while will work...

do
readFile >> variable;
while(!readFile.eof())

if you had not already checked empty file, a while loop of another sort is usually used:
while(readFile >> variable)
{
//any additional processing of variable here
}






When posting code, please use code tags. Highlight your code and click the <> button to the right of the edit window.

class Book; should be class Book (no semicolon).

Let's work from the outside in. It would be nice to read and write the books like this:
1
2
3
4
5
6
    char ch;
    Book book;
    while (cin >> book) {
        cin.get(ch);            // skip newline
        cout << book << '\n';
    }

Here I have somewhat arbitrarily decided that the code that reads and writes a book will not process the end of line. So I read the newline character with cin.get(ch) and write it with << '\n';

Now you need the << and >> operators. The definition is a little funny:
1
2
3
4
5
istream & operator >> (istream &is, Book &book)
{
    // code to read
    return is;
}

and
1
2
3
4
5
ostream &operator << (ostream &os, const Book &book)
{
    // code to write
    return os;
}


To read a string from a stream called is, stopping at a comma, you use:
1
2
string str;
getline(is, str, ',')

This reads characters into str until it sees a comma (or end of file). Then it throws away the comma without storing it.

After skipping the comma, sometimes you have to skip the space that follows it. You can do that with
1
2
char ch;
is.get(ch);  // read one character 

If you're reading a number with is >> num then you don't have to skip the space, is >> num will do that for you.

Hopefully this will be enough info for you to write a program to read and write the records (without sorting them. We'll do that later).
Looks like everyone was out sick that day. And now the professor has caught it too. I hate when that happens.

Topic archived. No new replies allowed.