0

I have a text file that has multiple sets of book information (title, author, etc). I need to be able to use a loop to read from the file and assign each piece of info to a corresponding string. I have it working to where I goes through the entire file, it's just messing up while going through the file.

Book readOne(ifstream &fin) {
string titleOne;
getline(fin, titleOne, ',');
string firstOne;
getline(fin, firstOne, ',');
string lastOne;
getline(fin, lastOne, ',');
string formatOne;
getline(fin, formatOne, ',');
string pubDateOne;
getline(fin, pubDateOne, ',');
string priceOne;
getline(fin, priceOne);

Here is the text file:

Gone With the Wind, Margaret Mitchell, Hardcover, 1936, 17.49
The Adventures of Sherlock Holmes, Arthur Doyle, Paperback, 1892, 6.85
The Illustrated A Brief History of Time, Stephen Hawking, Hardcover, 1996, 9.59
Frankenstein, Mary Shelley, Paperback, 1818, 7.99 
Command Authority, Tom Clancy, Paperback, 2013, 15.99
Origin, Dan Brown, Ebook, 2017, 14.99
The Lost Order, Steve Berry, Audiobook, 2017, 5.95
The Hunt for Red October, Tom Clacy, Audiobook, 1984, 7.00
Patriot Games, Tom Clancy, Audiobook, 1987, 22.50
The 14th Colony, Steve Berry, Paperback, 2016, 9.99
The Bishop's Pawn, Steve Berry, Ebook, 2018, 14.99
Pride and Prejudice, Jane Austen, Ebook, 1813, 8.99
Sense and Sensibility, Jane Austen, Hardcover, 1811, 19.99
Wuthering Heights, Emily Bronte, Paperback, 1847, 6.99
Jane Eyre, Charlotte Bronte, Hardcover, 1847, 10.95 
Anna Karenina, Leo Tolstoy, Paperback, 1877, 5.99
Sahara, Clive Cussler, Ebook, 1992, 5.99
The Notebook, Nicholas Sparks, Hardcover, 1996, 12.59
A Walk to Remember, Nicholas Sparks, Ebook, 1999, 7.99
See Me, Nicholas Sparks, Ebook, 2015, 7.99
The Last Song, Nicholas Sparks, Paperback, 2009, 5.99
The Wedding, Nicholas Sparks, Ebook, 2003, 7.99

My thinking was that it would read until a comma, assign that piece of info to the string, then continue. Instead, it outputs as if it didn't see certain commas.

Gone With the Wind by  Margaret Mitchell  Hardcover on  1936. Published on  17.49
The Adventures of Sherlock Holmes. It costs $0
The Illustrated A Brief History of Time by  Stephen Hawking  Hardcover on  1996. Published on  9.59
Frankenstein. It costs $0
Command Authority by  Tom Clancy  Paperback on  2013. Published on  15.99
Origin. It costs $0
The Lost Order by  Steve Berry  Audiobook on  2017. Published on  5.95
The Hunt for Red October. It costs $0
Patriot Games by  Tom Clancy  Audiobook on  1987. Published on  22.50
The 14th Colony. It costs $0
The Bishop's Pawn by  Steve Berry  Ebook on  2018. Published on  14.99
Pride and Prejudice. It costs $0
Sense and Sensibility by  Jane Austen  Hardcover on  1811. Published on  19.99
Wuthering Heights. It costs $0
Jane Eyre by  Charlotte Bronte  Hardcover on  1847. Published on  10.95
Anna Karenina. It costs $0
Sahara by  Clive Cussler  Ebook on  1992. Published on  5.99
The Notebook. It costs $0
A Walk to Remember by  Nicholas Sparks  Ebook on  1999. Published on  7.99
See Me. It costs $0
The Last Song by  Nicholas Sparks  Paperback on  2009. Published on  5.99
The Wedding. It costs $0
  • Have a look here for a `while` loop https://stackoverflow.com/a/7868998/2836621 and https://stackoverflow.com/a/19473883/2836621 here for loading an array. – Mark Setchell Dec 06 '17 at 06:14

1 Answers1

0

This is nothing but a csv (comma separated value) file. And there are ample code samples to read it. Refer this sample.