reading data from a file into vectors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 32
Reputation: hezfast2 is an unknown quantity at this point 
Solved Threads: 0
hezfast2 hezfast2 is offline Offline
Light Poster

reading data from a file into vectors

 
0
  #1
May 3rd, 2008
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.

[code]
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);
}
}
[code]
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: reading data from a file into vectors

 
0
  #2
May 3rd, 2008
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.
  1. struct Item
  2. {
  3. string ID;
  4. string Name;
  5. double Price;
  6. };
  7.  
  8. void getData(ifstream& infile, vector < Item > & Items)
  9. {
  10. }
  11.  
  12. int main()
  13. {
  14. vector < Item > Items;
  15. ifstream infile("file.txt");
  16. getData(infile, Items);
  17. return 0;
  18. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 32
Reputation: hezfast2 is an unknown quantity at this point 
Solved Threads: 0
hezfast2 hezfast2 is offline Offline
Light Poster

Re: reading data from a file into vectors

 
0
  #3
May 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: reading data from a file into vectors

 
0
  #4
May 3rd, 2008
>>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.
Last edited by Lerner; May 3rd, 2008 at 7:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 32
Reputation: hezfast2 is an unknown quantity at this point 
Solved Threads: 0
hezfast2 hezfast2 is offline Offline
Light Poster

Re: reading data from a file into vectors

 
0
  #5
May 3rd, 2008
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?

  1. 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)
  2. {
  3. int sold = 0;
  4. string name;
  5. int id, ordered;
  6. double mPrice;
  7. double sPrice;
  8. char ch;
  9.  
  10. while (infile >> id)
  11. {
  12. infile.get(ch);
  13.  
  14. getline(infile, name);
  15.  
  16. infile >> ordered >> mPrice >> sPrice;
  17.  
  18.  
  19. itemID.pushback(id);
  20. itemName.pushback(name);
  21. pOrdered.pushback(ordered);
  22. pInStore.pushback(ordered);
  23. pSold.pushback(sold);
  24. manufPrice.pushback(mPrice);
  25. sellingPrice.pushback(sPrice);
  26. }
  27. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: reading data from a file into vectors

 
0
  #6
May 4th, 2008
Originally Posted by hezfast2 View Post
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?

  1. 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)
  2. {
  3. int sold = 0;
  4. string name;
  5. int id, ordered;
  6. double mPrice;
  7. double sPrice;
  8. char ch;
  9.  
  10. while (infile >> id)
  11. {
  12. infile.get(ch);
  13.  
  14. getline(infile, name);
  15.  
  16. infile >> ordered >> mPrice >> sPrice;
  17.  
  18.  
  19. itemID.pushback(id);
  20. itemName.pushback(name);
  21. pOrdered.pushback(ordered);
  22. pInStore.pushback(ordered);
  23. pSold.pushback(sold);
  24. manufPrice.pushback(mPrice);
  25. sellingPrice.pushback(sPrice);
  26. }
  27. }
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:

  1. while (infile >> id)
  2. {
  3. infile >> name >> ordered >> mPrice >> sPrice;
  4. // code to put the info in appropriate vectors
  5. }

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:
  1. 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/s...push_back.html
Last edited by VernonDozier; May 4th, 2008 at 1:45 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC