Hello again, I'm trying to finish a function I am working on that reads data from a file set up like:

itemID
itemName
pOrdered manufPrice sellingPrice

itemID
itemName
pOrdered manufPrice sellingPrice

....and so on

and puts the data into vectors

this is what I've gotten so far and I'm not sure I am going about this in the right way, the difference between using arrays and vectors is confusing the heck out of me; pSold is supposed to be 0 initially and pInStore is supposed to be the same as pOrdered, that is why they are not in the input file. Any further guidance would be greatly appreciated.

void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice)
{
    int sold = 0;
    string name;
    int id, ordered;
    double mPrice;
    double sPrice;
    char ch;

    infile >> id;         

    infile.get(ch);                                

    getline(infile, name);       

    infile.get(ch);

    getline(infile, ordered, mPrice, sPrice); 

    while (infile)

    {
        itemID.pushback(id);
        itemName.pushback(name);
        pOrdered.pushback(ordered);
        pInStore.pushback(ordered);
        pSold.pushback(sold);
        manufPrice.pushback(mPrice);
        sellingPrice.pushback(sPrice);
     }
}

Just a suggestion, have you considered using a struct/class the encapsulates the data of a single item? You could then operate with a vector of such struct/class instead of the many separate vectors you have now. E.g.

struct Item
{
    string ID;
    string Name;
    double Price;
};

void getData(ifstream& infile, vector < Item > & Items)
{
}

int main()
{
     vector < Item > Items;
     ifstream infile("file.txt");
     getData(infile, Items);
     return 0;
}

I have to use seven vectors in this program, I just wanted to make sure I have the function set up correctly so that the data goes into the appropriate vectors.

>>getline(infile, ordered, mPrice, sPrice);

To my knowledge there is no version of getline() that takes four parameters, and no version of getline() that stores information in ints or doubles. To my knowledge getline() is only for strings . Therefore I'd suggest a series of three calls to >> to obtain the information for ordered, mPrice and sPrice.

The while loop should start before this line:

infile >> id;

In fact the above line should actually be the conditional statement for the while loop, like this:

while(infile >> id)

BTW: Thanks for using code tags. Unfortunately you forgot to use the / in front of the word code within the [] as the closing tag. I've done that on more than one occassion, too. Next time you'll know to review your code after posting to look for that so you can edit your code and put it in.

Would this be the correct way to do it? I'm having some difficulty following the logic of having the infile >> id as the while condition. could you explain how it works?

void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice)
{
	int sold = 0;
    string name;
    int id, ordered;
    double mPrice;
    double sPrice;
    char ch;

	while (infile >> id)
	{
		infile.get(ch);                                

        getline(infile, name);       

	    infile >> ordered >> mPrice >> sPrice; 

    
	    itemID.pushback(id);
	    itemName.pushback(name);
	    pOrdered.pushback(ordered);
	    pInStore.pushback(ordered);
	    pSold.pushback(sold);
	    manufPrice.pushback(mPrice);
	    sellingPrice.pushback(sPrice);
     }
}

Would this be the correct way to do it? I'm having some difficulty following the logic of having the infile >> id as the while condition. could you explain how it works?

void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice)
{
	int sold = 0;
    string name;
    int id, ordered;
    double mPrice;
    double sPrice;
    char ch;

	while (infile >> id)
	{
		infile.get(ch);                                

        getline(infile, name);       

	    infile >> ordered >> mPrice >> sPrice; 

    
	    itemID.pushback(id);
	    itemName.pushback(name);
	    pOrdered.pushback(ordered);
	    pInStore.pushback(ordered);
	    pSold.pushback(sold);
	    manufPrice.pushback(mPrice);
	    sellingPrice.pushback(sPrice);
     }
}

I imagine Lerner is referring to something like this. I didn't see him mention how to handle name one way or the other, so I'm not sure. You'll need to read in four pieces of data inside the loop with >> rather than three since name is in the data as well. You have five pieces of data to read each time through the loop, not four, so read it in like this:

while (infile >> id)
	{
	    infile >> name >> ordered >> mPrice >> sPrice; 
            // code to put the info in appropriate vectors
        }

I don't see the need for the getline or the get commands. All the data is separated by white space, so C++ will figure it exactly where everything starts and stops (I'm assuming that name has no spaces in it?). When you use the >> operator, it's irrelevant whether data is separated by a newline or spaces. When you run out of data, the command infile >> id will fail, so that will bail you out of the while loop at the proper time.

If name IS the first and last name and it's separated by a space, then the above won't work and getline may be a good idea, in which case the way you have it should work (the way you have it should also work even if there are no spaces) . I don't know if you need this line:

infile.get(ch);

If it's chopping off a character, try deleting that line. There are a few versions of getline. I usually specify the '\n' delimiter, but I don't know that it matters. What happens when you run your program?

Also, the command is push_back, not pushback:
http://www.cplusplus.com/reference/stl/vector/push_back.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.