0
#include <iostream>
#include <fstream>
#include <string>

struct textbook //Declare struct type
{
    int ISBN;
    string title;
    string author;
    string publisher;
    int quantity;
    double price;
};

// Constants
const int MAX_SIZE = 100;

// Arrays
textbook inventory[MAX_SIZE];

void readInventory()
{
    // Open inventory file
    ifstream inFile("inventory.txt");

    // Check for error
    if (inFile.fail())
    {
        cerr << "Error opening file" << endl;
        exit(1);
    }

    // Loop that reads contents of file into the inventory array.

       pos = 0; //position in the array

       while (

        inFile >> inventory[pos].ISBN
               >> inventory[pos].title
               >> inventory[pos].author 
                   >> inventory[pos].publisher
               >> inventory[pos].quantity 
                   >> inventory[pos].price
          )

    {
        pos++;

    } 

    // Close file 
    inFile.close();

    return;

}

Hello,

I need assistance with this function. The goal of this function is to read in the information from a txt file and read it in to an array struct for a textbook. The text file itself is already set up in the correct order for the loop.

My problem is that for the title section, the title of a book might be multiple words like 'My first book" for example. I know that I have to use a getline to take the line as a string to feed it into the 'title' data type.

I am also missing a inFile.ignore() somewhere but I don not know how to put it into a loop.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
C. Timmerman
  • 37
  • 1
  • 3
  • 1
    can we see a sample txt file? what separates the title from the author? – Joseph Ireland Mar 22 '17 at 21:55
  • 1
    Unanswerable without a sample of the file or guesswork. Guess: you can test input validity with reads of chunks and the `&&` opperator: `if (inFile >> inventory[pos].ISBN && infile.ignore() && std::getline(inFile, inventory[pos].title) &&...)` but it may be better to read everything as `string`s with `getline`and convert the `string`s to numbers as required after the fact. – user4581301 Mar 22 '17 at 22:37
  • inventory.txt file: 20451 My First Book Mark Lusk Pearson Publishing 40 45.34 9780316 Brown Family Mason Victor Little Brown 36 105.99 1349877 Story of My Life Norah M Jones CreateSpace Independent Publishing Platform 20 18 – C. Timmerman Mar 25 '17 at 23:34

1 Answers1

-1

Presuming the input format is:

ISBN title author publisher price quantity price

i.e., the data members are line white space separated, you could define operator>> for your struct textbook, which could look something like:

std::istream& operator>> (std::istream& is, textbook& t)
{
    if (!is) // check input stream status
    {
        return is;
    }

    int ISBN;
    std::string title;
    std::string author;
    std::string publisher;
    int quantity;
    double price;

    // simple format check
    if (is >> ISBN >> title >> author >> publisher >> quantity >> price)
    {
          // assuming you additionally define a (assignment) constructor  
          t = textbook(ISBN, title, author, publisher, quantity, price);

          return is;
    }

    return is;
}

Then to read your file you simply do:

std::ifstream ifs(...);

std::vector<textbook> books;

textbook b; 
while (ifs >> b)
{
     books.push_back(b);
}

Regarding the use of getline() see this answer.


Community
  • 1
  • 1
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • 1
    If your assumptions about the file format are correct, the task is exceedingly hard as few authors and books have a single word name. Solving this problem with is far past the skills of anyone who would be asking a question like this. If you read further into the question, the OP talks about knowing they need to use getline. This suggests either there is a delimiter, shame on OP for not supplying a file sample, that can be used to be used with `getline`. The suggestion of the creation of use of `operator>>` is spot on, though, and saves this answer from a downvote. – user4581301 Mar 22 '17 at 22:32
  • I appreciate your feedback. This is part of a programming assignment for school that is very 'hands off' so I might be taking the wrong approach. The reason why I think I need to use getline is because the author and title line on the inventory file is an entire phrase so I need to get the entire string into the array. We were also given a hint to not forget to use .ignore() to remove the \n from the buffer. – C. Timmerman Mar 24 '17 at 11:02
  • @C.Timmerman I see what you mean. Have a look at `std::vector` instead of array, as you are using C++, not C. As for the `getline()`, it could be included in an implementation, but you should remember that there are multiple ways of doing what you want, even without its use, my answer is an example of that. Do you need further clarification/ help for your question? – Ziezi Mar 24 '17 at 11:17