d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Vector And Files
Add Reply New Topic New Poll
Member
Posts: 636
Joined: Mar 8 2009
Gold: 551.96
May 25 2012 05:48pm
I'm a little confused how to do this, I need to read in from a text file and then create a 'new Product' or 'new SaleProduct' depending if the first character is an 'R' or 'S' (regular/sale)
First we were told to create a vector which is vector(Product*) Inventory;

Then the example we were shown looks like this:
Code

inventory.push_back(new Product(descr, quantity, price));
inventory.push_back(new SaleProduct(descr, quantity, price, discount));


Now what I'm lost on, we have to distinguish if the first character in the file holds an R or S then do that push_back to the appropriate one (using an if statement i presume). But how do I do this when the file being read in, isn't even part of the vector yet? Sorry if this is confusing, tried explaining it the best i could.

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 25 2012 06:22pm
essentially you wanna read in one line at a time, parse it, create the appropriate object, add to vector, repeat
Member
Posts: 636
Joined: Mar 8 2009
Gold: 551.96
May 25 2012 06:48pm
Would you mind showing me how? Every time we've read in files we've always done the entire thing
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll