Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Inventory
- {
- string partNum;
- vector<int> quan;
- };
- #Read an entire line with getline(), then use the stringstream class to parse this string.
- #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.
- #http://www.cplusplus.com/doc/tutorial/structures/ if you need more info on structs
- #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 >>
- #http://www.cplusplus.com/doc/tutorial/files/ for more on file I/O
- #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.
- Example (really good starting place if nothing else):
- char comma;
- string temp;
- while(getline(temp,','))
- {
- // Use a temporary variable to make adding to the vector cleaner.
- // Add an empty element to your vector. Only do this if you are able to read the first item.
- v.push_back(Inventory());
- // Copy temp to this element's partNum.
- v.partNum = temp;
- file >> v.totalQuan;
- for(int i = 0; i < NumQuan; ++i)
- { // The variable comma is used to extract the comma and throw it away.
- file >> comma >> v.quan[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement