vector - Barcode file reading c++ -


forum. i've been bit stuck few days in lab assigned last week. it's supposed lab write code program acts pos (point of sale) system allowing user put items in cart , @ checkout display each item ordered, price, quantity , total.

here more detailed list of requirements assignment can whole picture:

  1. no global variables or global constants allowed in lab, except file name.

  2. your program must read inventory data stored in file labdata.txt , load data 4 separate vectors:

    • a. 1 vector barcodes
    • b. 1 vector product description
    • c. 1 vector quantity available (inventory)
    • d. 1 vector unit price (must contain prices 2 decimal places read file) not using vectors earn 0 lab. these parallel vectors.
  3. the data file provided has 4 items. file format is: bar code, item description, inventory quantity, , unit price must add 2 more items file use. use real items found in household in order store real barcodes , product names. can determine price , quantity available. when creating data, use website test have valid bar codes: http://www.searchupc.com file must have 6 items. keep in mind program tested files can have less or more items, hence not limit program specific number of items.

  4. create function named readdatafile, reads data file , stores data in vectors. data file must read c:\temp\. may specify prototype function in whatever way makes sense you, must take least 2 arguments. function should called main load data vectors.

  5. in main, prompt user bar code (or name) of item s/he wants purchase (determine user-friendly prompt). must have 1 cin , program must know whether bar code or item name entered , operate accordingly. highly recommended (however not required) write separate functions handle of following scenarios:

    • a. if item not in list of items, display error message , allow user continue shopping.
    • b. in cases desired item in list, prompt how many user wants buy , validate have enough units in inventory sell, otherwise display appropriate error message.
    • c. when enough inventory units available, reduce quantity purchased quantity available in vector , add bar code of item , quantity purchased shopping cart. may use data structure wish store shopping cart data. keep in mind shopping cart printed screen when program finishes , there not fixed number of items user can purchase.
    • d. user might want purchase more units of item s/he has purchased (is in shopping cart). in case, program should update quantity in shopping cart , in inventory vector accordingly.
    • e. allow user continue purchasing items until s/he enters 9999 bar code/item name. 9999 sentinel checkout. when happens, display items stored in shopping cart:

      • i. barcode
      • ii. product description
      • iii. quantity
      • iv. item total (quantity * price) output must produced calling separate function. can decide on function name, prototype, format of output, etc. must take @ least 1 argument.
  6. after user checkouts shopping cart, display grand total in main. might choose format , proper user-friendly message.

  7. document of functions
  8. not closing file

that assignments requirements.

now, got far making vectors program, i'm having problem understanding how vectors read text file , use text file's content user's inputted item accurately.

here code far

void readdatafile(vector<string>&, vector <double>&, vector <int>&, vector <string>&); int main() { vector<string>bar_code; vector<double>unit_price; vector<int>inventory_quantity; vector<string>item_description;  readdatafile(bar_code, unit_price, inventory_quantity, item_description); system("pause"); return 0;}  void readdatafile(vector<string>&bar_code, vector <double>& unit_price, vector <int>& inventory_quantity, vector <string>& item_description) { string bar_code; int unit_price; double inventory_quantity; string item_description;  ifstream inputfile; inputfile.open("c:\\temp\\data.txt"); while (!inputfile.eof()) {     inputfile >> bar_code;     bar_code.push_back(bar_code);      inputfile >> unit_price;     unit_price.push_back(unit_price);      inputfile >> inventory_quantity;     inventory_quantity.push_back(inventory_quantity);      inputfile >> item_description;     item_description.push_back(item_description);   } } 

also here text file provided first 4 items needed read:

367730154604 pringles 20 1.50

0164000031190 sour 10 2.50

391360056850 onion 35 2.97

040255682427 potato 23 2.98

thank in advance!

firstly, change data structures around it's not unnecessarily clumsy , error prone. 2015:

use struct:

struct record {     string bar_code;     double unit_price;     int inventory_quantity;     string item_description; };  vector<record> readdatafile(); 

next, implement stream extraction reading lines (getline) , extracting individual fields using istringstream.

don't ever use while (!s.eof()).

live demo

live on coliru

#include <iostream> #include <vector> #include <fstream> #include <sstream> #include <iterator>  using namespace std;  struct record {     string bar_code;     double unit_price;     int inventory_quantity;     string item_description;      friend ostream& operator<<(ostream&, record const&); };  vector<record> readdatafile();  int main() {     auto data = readdatafile();       copy(data.begin(), data.end(), ostream_iterator<record>(std::cout, "\n")); }  vector<record> readdatafile() {     vector<record> data;      ifstream inputfile("c:\\temp\\data.txt");      size_t linenumber = 0;     string line;     while (getline(inputfile, line)) {         linenumber += 1;          record r;         istringstream iss(line);          if (iss >> r.bar_code >> r.item_description >> r.inventory_quantity >> r.unit_price)             data.push_back(r);         else             throw runtime_error("parse error in line #" + to_string(linenumber) + " '" + line + "'");     }      return data; }  ostream& operator<<(ostream& os, record const& r) {     return os << r.bar_code << " " << r.item_description << " " << r.inventory_quantity << " " << r.unit_price; } 

prints input output.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -