1

So I am trying to create an array of objects (the Item object) in C++ by getting the information from an input file. The first line contains the number of items there are and the total capacity of a bag to store them. Every line after that contains the name of an item, the profit, and the weight with spaces in between.

Example of an input file:

7 25
orange 50 5
banana 60 10
kitchensink 140 20
strawberry 100 14
tangerine 10 5
puppy 25 12
grape 30 7

How do I set up my program/constructor so it's not reading only the first line and will read the items in line by line?

#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;

static ifstream fr;
//class for item object
class Item
{
public:
    //constructor that will get/initialize the name, weight, 
    //profit of item from input file and initialize/calculate the ratio
    Item()
    {
        fr>>name>>p>>w;
        r=p/w;
    }

    string getName()
    {return name;}

    int getWeight()
    {return w;}

    int getProfit()
    {return p;}

    double getRatio()
    {return r;}

private:
    string name;
    int w, p;
    double r;
};


int main()
{
    int n, c;

    //ifstream fr;
    fr.open("inputp1.txt");
    fr>>n>>c; //get n from input file //get capacity


    //create an array of Items objects reading in from the input file based on n
    Item tosteal[n];
    for (int i=0; i<n; i++)
    {
        tosteal[i]= Item();
    }
    fr.close();

    for (int i=0; i<n; i++)
    {
        cout <<"Profit: "<<tosteal[i].getProfit()<<endl;
        cout <<"Weight: "<<tosteal[i].getWeight()<<endl;
        cout <<"Name: "<<tosteal[i].getName()<<endl;
        cout <<"Ratio: "<<tosteal[i].getRatio()<<endl;
    }
}
Megumi Ai
  • 89
  • 9
  • You've got a class for _an_ item. You dont want to change that constructor to read in more than 1 at a time, but rather you'd create another class that represents a collection of items. In that class's constructor you could open the file and repeatedly create new items.You should also make the ifstream variable a parameter to the constructor of the item class, remove the global instance of it and place one inside the class the represents the collection of items, – enhzflep Oct 01 '16 at 20:01
  • Can't a collection of objects be represented as an array of items and each spot in the array is an different item? ie tosteal[0] is an item with the name orange, profit of 50 and a weight of 5 and tosteal[1] is an item with the name banana, profit of 60, and weight of 10 – Megumi Ai Oct 01 '16 at 20:06
  • Sure, it can. I'm coming at it from the perspective of - if you're going to make the item a class that holds data and can do things, why not do the same with the collection? I've written some code that makes the suggested changes. (which I can now no longer post :( ) – enhzflep Oct 01 '16 at 20:49
  • Nevermind... we figured out that our constructor was reading it correctly. It just was "overwritten" when we called the tosteal[i]=Item() and the file had nothing else to read so it filled in the values with nulls and zeros. We did not realize that when we declared the array initially the constructor was called automatically. – Megumi Ai Oct 01 '16 at 21:50
  • For interest's sake - here's something to play with. It has a 24 hour expiry. http://pastebin.com/QPbrga02 – enhzflep Oct 01 '16 at 21:53

0 Answers0