Help with file input and appending a file

I am trying to read a file that has a list of books with their cost and price sold for then append the file such that it produces a file that has profit and revenue.

I am trying to create an object within a while loop and call it to append the file within the same loop.

I also need to disregard the first line in the file because it is just headers for the list that follows

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
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include "Book.h"

using namespace std;

int main()
{
	string title, author, ISBN;
	double price, cost;
	int QoH, QS;

	ifstream InClientFile{ "Book_List.txt", ios::in };

	if (!InClientFile) {
		cerr << "File could not be opened" << endl;
		exit(EXIT_FAILURE);
	}
	ofstream appClientFile;
	appClientFile.open("Book_List.txt", ios::app);

	if (!appClientFile) {
		cerr << "File could not be opened" << endl;
		exit(EXIT_FAILURE);
	}
	

	while (InClientFile >> title >> author >> price >> QoH >> QS >> ISBN >> cost)
	{
		Book book{ title, author, price, QoH, QS, ISBN, cost };
		appClientFile << book.getRevenue() << book.getProfit() << endl;
	}
	InClientFile.close();
	appClientFile.close();



}


This is the file (string, string, double, int, int, string, double):

"Title" "Author" "Price" "Qty on Hand" "Qty Sold" "ISBN" "Cost"
"Macbeth" "William Shakespeare" "41.04" "161" "23" "978-88-5985-004-5" "27.50"
"A Christmas Carol" "Charles Dickens" "98.74" "167" "547" "978-26-2885-780-7" "65.00"
closed account (48T7M4Gy)
I also need to disregard the first line in the file because it is just headers for the list that follows

At line 30 insert a line using getline() and assign output to a string called header (or dummy). Then continue on with reading in the data via your while loop.
Could you put a line of pseudo code? I am really not getting how to format it.
Thanks.
On line 30:
Discard the line:
{ std::string discard; std::getline(InClientFile, discard); }
I feel bad for constantly asking, but I am at a total loss. My code is aborting when I try to run it. I don't understand what is going on.

client.cpp

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include "Book.h"

using namespace std;

int main()
{
	string title, author, ISBN, price, cost, QoH, QS;
	double priceD, costD;
	int iQoH, iQS;

	ifstream InClientFile{ "Book_List.txt", ios::in };

	if (!InClientFile) {
		cerr << "File could not be opened" << endl;
		exit(EXIT_FAILURE);
	}
	ofstream appClientFile;
	appClientFile.open("Book_List.txt", ios::app);

	if (!appClientFile) {
		cerr << "File could not be opened" << endl;
		exit(EXIT_FAILURE);
	}
	

	string discard1;

	getline(InClientFile, discard1);

	
	while (InClientFile >> title >> author >> price >> QoH >> QS >> ISBN >> cost)
	{
		priceD = stod(price);
		costD = stod(cost);
		iQoH = stoi(QoH);
		iQS = stoi(QS);

		Book book{ title, author, priceD, iQoH, iQS, ISBN, costD };
		appClientFile << book.getRevenue() << ' ' << book.getProfit() << endl;
	}
	InClientFile.close();
	appClientFile.close();
}


Book.h
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
class Book
{
public:
	Book(string, string, double, int, int, string, double);
	void setTitle(string title);
	void setAuthor(string author);
	void setPrice(double price);
	void setQoH(int QoH);
	void setQS(int QS);
	void setISBN(string ISBN);
	void setCost(double cost);

	string getTitle();
	string getAuthor();
	double getPrice();
	int getQoH();
	int getQS();
	string getISBN();
	double getCost();
	double getRevenue();
	double getProfit();

private:
	string Title, Author, isbn;
	int qOh, qs;
	double Price, Cost;
};


Book.cpp
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
#include <string>
#include "Book.h"

using namespace std;

Book::Book(string title, string author, double price, int QoH, int QS, string ISBN, double cost)
{
	setTitle(title);
	setAuthor(author);
	setPrice(price);
	setQoH(QoH);
	setQS(QS);
	setISBN(ISBN);
	setCost(cost);
}

void Book::setTitle(string title)
{
	Title = title;
}

void Book::setAuthor(string author)
{
	Author = author;
}

void Book::setPrice(double price)
{
	Price = price;
}

void Book::setQoH(int QoH)
{
	qOh = QoH;
}

void Book::setQS(int QS)
{
	qs = QS;
}

void Book::setISBN(string ISBN)
{
	isbn = ISBN;
}

void Book::setCost(double cost)
{
	Cost = cost;
}

string Book::getTitle()
{
	return Title;
}

string Book::getAuthor()
{
	return Author;
}

double Book::getPrice()
{
	return Price;
}

int Book::getQoH()
{
	return qOh;
}

int Book::getQS()
{
	return qs;
}

string Book::getISBN()
{
	return isbn;
}

double Book::getCost()
{
	return Cost;
}

double Book::getRevenue()
{
	return Price * (double)qs;
}

double Book::getProfit()
{
	return ((Price * (double)qs) - (Cost * (double)qs));
}


Book_List.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"Title" "Author" "Price" "Qty on Hand" "Qty Sold" "ISBN" "Cost"
"Macbeth" "William Shakespeare" "41.04" "161" "23" "978-88-5985-004-5" "27.50"
"A Christmas Carol" "Charles Dickens" "98.74" "167" "547" "978-26-2885-780-7" "65.00"
"A Tale of Two Cities" "Charles Dickens" "67.00" "605" "740" "978-34-6709-227-6" "49.00"
"King Solomon's Mines" "Rider Haggard" "82.90" "211" "140" "978-83-3553-918-2" "75.00"
"The Hunchback of Notre-Dame" "Victor Hugo" "66.09" "371" "70" "978-26-2534-541-1" "55.50"
"Adventures of Tom Sawyer" "Mark Twain" "76.79" "119" "698" "978-66-9189-530-8" "70.00"
"The Brothers Karamazov" "Fyodor Dostoyevsky" "89.98" "31" "322" "978-74-3526-956-0" "80.00"
"The Jungle Book" "Rudyard Kipling" "93.97" "945" "725" "978-16-0018-998-2" "80.00"
"Curious George" "H.A. Rey" "23.06" "61" "158" "978-12-0539-413-6" "18.35"
"Gulliver's Travels" "Jonathan Swift" "51.05" "512" "198" "978-56-9564-422-5" "41.00"
"The Complete Calvin and Hobbes" "Bill Watterson" "56.44" "172" "528" "978-84-2205-509-8" "50.00"
"Jane Eyre" "Charlotte Bronte" "64.71" "764" "473" "978-00-9210-567-3" "32.00"
"Robinson Crusoe" "Daniel Defoe" "51.54" "239" "348" "978-50-2079-644-0" "35.75"
"David Copperfield" "Charles Dickens" "37.10" "503" "712" "978-60-9545-400-7" "27.50"
"The Count of Monte Cristo" "Alexandre Dumas" "55.72" "956" "944" "978-51-5888-666-5" "45.00"
"The Scarlet Letter" "Nathaniel Hawthorne" "49.97" "300" "721" "978-12-1285-170-1" "43.33"
"The Call of the Wild" "Jack London" "84.23" "188" "897" "978-54-8764-846-0" "78.85"
"The Merchant of Venice" "William Shakespeare" "36.73" "672" "61" "978-62-8444-969-5" "27.50"
"20,000 Leagues Under the Sea" "Jules Verne" "38.02" "914" "898" "978-27-0762-094-6" "27.50"
"War and Peace" "Leo Tolstoy" "83.01" "38" "250" "978-50-5952-067-6" "80.00"
"The Hobbit" "J.R.R. Tolkien" "90.49" "771" "681" "978-62-0444-519-2" "80.00"
"The Tale of Peter Rabbit" "Beatrix Potter" "58.39" "996" "635" "978-14-4946-950-4" "45.69"
"Black Beauty" "Anna Sewell" "33.20" "386" "758" "978-40-6271-424-6" "28.70"
closed account (48T7M4Gy)
The problem you have is the format of the data. Were you supplied with the text file with values delimited by quotation marks or is that how you devised it?

The essence of the solution is to read each line and then proceed to parse/break up that line to separate the value of the variables.

This gives you a start:
1
2
3
4
5
std::string line;
    while ( getline(InClientFile, line, '"') )
    {
        std::cout << line << '\n';
    }
Last edited on
closed account (48T7M4Gy)
http://en.cppreference.com/w/cpp/string/basic_string/getline

stringstreams are also very useful in these situations of parsing a line.
Last edited on
closed account (48T7M4Gy)
Extending a simple fix a bit further:
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
string dummy;
    getline(InClientFile, dummy);
    
    // Book(string title, string author, double price, int QoH, int QS, string ISBN, double cost)
    
    std::string aTitle, aAuthor, aPrice, aQOH, aQS, aISBN, aCost;
    
    while (
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aTitle, '"')
           and
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aAuthor, '"')
           and
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aPrice, '"')
           and
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aQOH, '"')
           and
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aQS, '"')
           and
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aISBN, '"')
           and
           getline(InClientFile, dummy, '"')
           and
           getline(InClientFile, aCost, '"')
           
           )
    {
        std::cout << "Book title: " << aTitle << '\n';
        std::cout << "    Author: " << aAuthor << '\n';
        std::cout << "     Price: " << aPrice << '\n';
        std::cout << "       QOH: " << aQOH << '\n';
        std::cout << "        QS: " << aQS << '\n';
        std::cout << "      ISBN: " << aISBN << '\n';
        std::cout << "      Cost: " << aCost << '\n';
    }
Thank You for all your help. I was provided the text file and therefore forced to use the values stored in quotation marks.
Topic archived. No new replies allowed.