Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. struct Inventory
  2.     {
  3.        string partNum;
  4.        vector<int> quan;
  5.     };
  6.  
  7. #Read an entire line with getline(), then use the stringstream class to parse this string.
  8.  
  9. #You have an inventory "record" on each line. Everything on one line is related. This is where a structure comes into play. You use #one record, a structure, to contain everything on one line. Say your structure is named Inventory, you then use a vector<Inventory> #to hold the entire inventory.
  10.  
  11. #http://www.cplusplus.com/doc/tutorial/structures/ if you need more info on structs
  12.  
  13. #There are several ways to retrieve information from a file. On way is to treat every thing as a string, the other is to retrieve the #information into the correct data type using the extraction operator >>
  14.  
  15. #http://www.cplusplus.com/doc/tutorial/files/ for more on file I/O
  16.  
  17. #extract the file to your structure variables. Use getline() to extract your string, then just use the extraction operator>> to #extract all the rest of the variables.
  18.  
  19. Example (really good starting place if nothing else):
  20.  
  21.     char comma;
  22.     string temp;
  23.     while(getline(temp,','))
  24.     {
  25.         // Use a temporary variable to make adding to the vector cleaner.
  26.         // Add an empty element to your vector. Only do this if you are able to read the first item.
  27.         v.push_back(Inventory());
  28.  
  29.        // Copy temp to this element's partNum.
  30.        v.partNum = temp;
  31.        file >> v.totalQuan;
  32.        for(int i = 0; i < NumQuan; ++i)
  33.        {   // The variable comma is used to extract the comma and throw it away.
  34.           file >> comma >> v.quan[i];
  35.        }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement