parsing file data into array of objects

assignment is as follows:

Fred Barnes and his partner Alfred Noble are trying to drum up some business for their newly launched bookstore. They have decided to run a special on a few classic titles and now need your help to write a program that will help them monitor their progress. To accomplish this, they would like to have reports (read that as files) for the following:
1) The top 10 best selling books (sorted by descending volume). Output format:
Title Author Price Qty Sold Macbeth William Shakespeare 41.04 23
2) The top 10 revenue producing books (sorted by descending revenue). Output format:
Title Author Price Qty Sold Macbeth William Shakespeare 41.04 23
ISBN 978-88-5985-004-5
Revenue $$
3) The top 10 most expensive books (sorted by descending price). Output format:
Title Author Price ISBN
Macbeth William Shakespeare 41.04 978-88-5985-004-5
4) All the information sorted by book title in alphabetical order. Output format:
"Title","Author","Price","Qty on Hand","Qty Sold","Revenue","ISBN"
"A Christmas Carol","Charles Dickens","98.74","167","547","$$","978-26-2885-780-7"
"A Tale of Two Cities","Charles Dickens","67.00","605","740","$$","978-34-6709-227-6"
"King Solomon's Mines","Rider Haggard","82.90","211","140","$$","978-83-3553-918-2"
"Last of the Mohicans","James Fenimore Cooper","87.66","93","$$","974","978-32-6337-721-7" "Macbeth","William Shakespeare","41.04","161","23","$$","978-88-5985-004-5"
Replace the $$ with the actual revenue numbers!
You will use the Book_List.csv file provided on Blackboard as the data for this assignment.

heres what i have so far:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// function to sort and output data to a new file
//  - overloaded << operator
#include <iostream>
#include <fstream>
#include <string>
#include <streambuf>

const int MAX_CHARS_PER_LINE = 1000;
const int MAX_TOKENS_PER_LINE = 5;
const char* const DELIMITER = ",";

using namespace std;

std::fstream t("Book_List.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                std::istreambuf_iterator<char>());

template<typename T>
class Counter {
public:
    Counter() { ++count; }
    Counter(const Counter&) { ++count; }
    ~Counter() { --count; }
    
    static size_t howMany()
    { return count; }
    
private:
    static size_t count;
};

template<typename T>
size_t
Counter<T>::count = 0; // this now can go in header

template<class T>
void sort(T* v,int n)
{
    for (int i = 1; i < n; i++)
        for (int j = 0; j=0, j< n-i; j++)
            if (v[j] > v[j+1])
                swap (v[j],v[j+1]);
}
template<class T>
int findLines(string)
{
    int numLines = 0;
    ifstream in("file.txt");
    //while ( ! in.eof() )
    while ( in.good() )
    {
        std::string line;
        std::getline(in, line);
        ++numLines;
    }
    return numLines;
}

class Book
{
public:
    Book();
    Book(int);
    Book(string);
	Book(long, long);
    Book(const Book&);
    Book(string, string, string, double, double, double);
    
    void setBookData(string, string, string, double, double, double);
	void setAuthor(string);
	void setTitle(string);
	void setISBN(string);
	void setQS(double);
	void setPrice(double);
	void setQOH(double);
    void getBookData(string, string, string, double, double, double);                                               // friend ostream& operator=(ifstream &, string);
    Book& operator = (const Book&);
    Book operator = (string);                       //book will cout string
	Book& operator = (ifstream &);
    friend istream& operator >> (istream &, const Book&);
    friend ostream& operator << (ostream &, const Book&);
    
    int a;
    string num;
	long end, begin;
	
private:
    string Title, Author, ISBN;
    double Price, QOH, QS;
    
};


Book :: Book()
{ }
Book :: Book(int i)
{
    a = i;
}
Book :: Book(string z) : num(z)
{ }
Book :: Book(long b, long e)
{
	b = 0;
	e = 0;
}
Book :: Book(const Book& v) : Title(v.Title), Author(v.Author), Price(v.Price), QOH(v.QOH), QS(v.QS), ISBN(v.ISBN)
{ } // copy constructor
Book :: Book(string t, string a, string isbn, double p, double qoh, double qs )
{
	Title = t;
	Author = a;
	ISBN = isbn;
	Price = p;
	QOH = qoh;
	QS = qs;
}
void Book :: setBookData(string t, string a, string isbn, double p, double qoh, double qs)
{
	Title = t;
	Author = a;
	Price = p;
	QOH = qoh;
	QS = qs;
	ISBN = isbn;
}
void Book :: getBookData(string t, string a,string isbn, double p, double qoh, double qs)
{
	t = Title;
	a = Author;
	isbn = ISBN;
	p = Price;
	qoh = QOH;
	qs = QS;
}
void Book :: setAuthor(string t)
{
	Author = t;
}
void Book :: setISBN(string i)
{
	ISBN = i;
}
void Book :: setTitle(string a)
{
	Title = a;
}
void Book :: setPrice(double p)
{
	Price = p;
}
void Book :: setQS (double q)
{
	QS = q;
}
void Book :: setQOH( double qo)
{
	QOH = qo;
}
Book& Book::operator=(const Book& v) //passed in from copy constructor
{
	if (this != &v)
	{//if they are the same object, we can skip copying of x
		//if different copy into this instance
        Title =	v.Title;
        Author = v.Author;
        Price = v.Price;
        QOH = v.QOH;
        QS = v.QS;
        ISBN = v.ISBN;
	}
	return *this; //return dereferenced instance of Fractionv to enable cascading assignment.
}
/*
 void Book operator=(ifstream& ifs, string s)
 {
 Book temp;
 temp.num = s;
 return temp;
 } */

istream& operator >> (istream &is, const Book &v)
{
	cout << "enter any number: " << endl;
    is >> v.num;
    return is;
}
ostream &operator << (ostream &os, const Book&v)
{
    os<< v.Title << v.Author << v.ISBN << v.Price << v.QOH << v.QS;
    return os;
}
int main()
{
	int num;
	long begin,end;
	begin = 0;
	end = 0;
    string bookstring;
	Book BookS[5];
    
    // create a file-reading object
    ifstream indata;
    indata.open("Book_List.csv"); // open a file
    if (!indata.good())
        return 1; // exit if file not found
    while (!indata.eof())
    {
        char buf[MAX_CHARS_PER_LINE];
        indata.getline(buf, MAX_CHARS_PER_LINE);
        int n = 0;
        const char* token[MAX_TOKENS_PER_LINE] = {0}; // initialize to 0
        // parse the line
        token[0] = strtok(buf, DELIMITER); // first token
        if (token[0]) // zero if line is blank
        {
			for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
            {
                token[n] = strtok(0, DELIMITER); // subsequent tokens
                if (!token[n] || token[n]==0) break; // no more tokens
                
            }
        }
        
        // process (print) the tokens
		for (int n = 0; n < MAX_TOKENS_PER_LINE; n++) // n = #of tokens
        {
            cout << "Token[" << n << "] = " << token[n] << endl;
            cout << endl;
            
        }
        
		indata.close();
        
	}
    cin.get();
	return 0;
    
}

// function to find out how many objects to create (function find)

// create an array (of tokens) with that many objects (function create)
//  - use seekg to get to the beginning of file before starting (sort through file)

// function to read information from file into array of objects (read content into array of objects)
//  - use substr in the parsing process
//  - use atoi and atof in the parsing process

// loop to call sorting function based on 3 different criteria (3 sort criteria)
//  - sorting function will call a function template to compare values
//  - print results for each sort

// function to sort and output data to a new file
//  - overloaded << operator 


i know im a long way off, but im stuck in even how to begin parsing this bad boy. i need to create tokens for the 6 data members (Author,Title,Price,QOH,QS,Revenue) and then read in from the file "Book_List.csv" the number of lines of the file, which will tell me how many obects in the array of books i should create. Then I will instantiate the Book array and inputting values passed in from specific token values; if you can just help me to get started i could really use your help.

// function to find out how many objects to create (function find)

// create an array (of tokens) with that many objects (function create)
// - use seekg to get to the beginning of file before starting (sort through file)

// function to read information from file into array of objects (read content into array of objects)
// - use substr in the parsing process
// - use atoi and atof in the parsing process

// loop to call sorting function based on 3 different criteria (3 sort criteria)
// - sorting function will call a function template to compare values
// - print results for each sort

// function to sort and output data to a new file
// - overloaded << operator[/code]

Last edited on
can someone please help me.
create tokens for the 6 data members (Author,Title,Price,QOH,QS,Revenue)


1
2
3
4
5
6
7
8
9
10
11
string Author;
string title;
double price;
int QOH;
int QS;
double revenue;
string ISBN;

//Each line read from file as follows:

fileInput >> Author >> title >> price >> QOH >>QS >> revenue >> ISBN;
Last edited on
hmm, so like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	string ftitle, fauthor, fisbn;
	double fprice, fqoh,fqs;
	ftitle = "Title";
	fauthor = "Author";
	fisbn = "978-88-5985-004-5";
	fprice = 41.04;
	fqoh = 161;
	fqs = 23;

	fin>>ftitle>>fauthor>>fprice>>fqoh>>fqs>>fisbn;
	Book b1,b2(ftitle,fauthor,fisbn,fqoh,fqs,fprice),b3(b2),b4;
	Book barray[50];
	for (int i =0; i<50; i++)
	{
		barray[i].setBookData(ftitle,fauthor,fisbn,fqoh,fqs,fprice);
	}

is there a way to match the tokens with my variables? Thanks.
Topic archived. No new replies allowed.