For my homework assignment, I have to create a class titled "Book" and I need to read a file, of a max size of 2000 lines, line by line into the class type below and then sort each line into alphabetical order. However, I don't know how to approach reading the file into the array. I have posted what I have so far below and the input file. I also wouldn't mind any suggestions when it comes to outputting the array after it is sorted.
The input file would look something like this:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
class Book
{
private:
string ID;
string author;
string title;
int edition;
int year;
string ISBN;
public:
Book ()
{
ID = "";
author = "";
title = "";
edition = 0;
year = 1900;
ISBN = "";
}
Book (string id, string a, string t, int edt, int yr, string isbn)
{
ID = id;
author = a;
title = t;
edition = edt;
year = yr;
ISBN = isbn;
}
};
int main ()
{
int main ()
{
Book bookist[2000];
int size;
string temp;
ifstream input;
input.open ("bookinput.dat");
if (input.fail)
{
cout << "Error when opening file" << endl;
exit (1);
}
ofstream out;
out.open ("bookouput.dat") if (out.fail)
{
cout << "Error when opening output file" << endl;
exit (1);
}
(while getline(input, str)) {
size++;
}
for(int i = 0; i < size; i++) {
in >> booklist[i];
// I know this doesn't work but you get the idea
}
}
|