c++ - Filling structure from a file -
hey guys doing project @ school , need fill array of pointers info text doc:
4101 braeburn_reg 1 0.99 101.5 4021 delicious_gdn_reg 1 0.89 94.2 4020 delicious_gldn_lg 1 1.09 84.2 4015 delicious_red_reg 1 1.19 75.3 4016 delicious_red_lg 1 1.29 45.6 4167 delicious_red_sm 1 0.89 35.4 4124 empire 1 1.14 145.2 4129 fuji_reg 1 1.05 154.5 4131 fuji_x-lge 1 1.25 164.1 4135 gala_lge 1 1.35 187.7 4133 gala_reg 1 1.45 145.2 4139 granny_smith_reg 1 1.39 198.2 4017 granny_smith_lge 1 1.49 176.5 3115 peaches 1 2.09 145.5 4011 bananas 1 0.49 123.2 4383 minneolas 1 0.79 187.3 3144 tangerines 1 1.19 135.5 4028 strawberries_pint 0 0.99 104 4252 strawberries_half_case 0 3.99 53 4249 strawberries_full_case 0 7.49 67 94011 organic_bananas 1 0.99 56.3
so have take put in struct. here function doing so:
bool readinventory(string filename) { inputfile.open(filename); //opening products file bool errors = true; if(!inputfile.fail()) // validate file did open { while (!filename) // dynamically creates array , fills info { product *inventory = new product(); inputfile >> inventory->plu; inputfile >> inventory->itemname; inputfile >> inventory->saletype; inputfile >> inventory->price; inputfile >> inventory->currentinventory; itemarray[counter] = inventory; counter++; cout << itemarray[14]<< endl; } } else { cout << "\nerror, unable open products.txt.\n"; errors = false; return errors; } }// ends readinventory
it wont fill in array, if while (filename) // dynamically creates array , fills info
record first item array only, leaving others blank. need validate input. (i.e plu code not integer, plu type string though) , skip product problems , change bool errors
true false. here whole code:
#include <iostream> #include <string> #include <fstream> #include <cctype> using namespace std; bool readinventory(string filename); double checkout(); bool updateinventory(string filename); // prototypes int counter = 0; //used fill array ifstream inputfile; //file read const int sizes = 100; //constant int used tell sizes product *itemarray[sizes]; //array of pointers of size 100 struct product { string plu; //price code each product string itemname; //name of item int saletype; //item per pound or per unit int price; //price of item int currentinventory; //amount in stock }; int main () { int choice; // choice used find out choice want in menu. bool menuon = true; //menuon used turn on , off menu. readinventory("products.txt"); while (menuon == true) { cout << "1 - check out.\n"; cout << "2 - close store , exit.\n"; cout << "enter choice , press return: "; //displays choices menu cin >> choice; //chose menu option want cout << endl; switch (choice) { case 1: checkout(); break; case 2: cout << "thank using item check out program."; menuon = false; break; default: cout << "not valid choice. \n"; cout << "choose again.\n\n"; break; // safty net if user doent enter choice 1-2 } } inputfile.close(); cout << endl; system("pause"); return 0; } // end function main () bool readinventory(string filename) { inputfile.open(filename); //opening products file bool errors = true; if(!inputfile.fail()) // validate file did open { while (counter < 22) // dynamically creates array , fills info { product *inventory = new product(); inputfile >> inventory->plu; inputfile >> inventory->itemname; inputfile >> inventory->saletype; inputfile >> inventory->price; inputfile >> inventory->currentinventory; itemarray[counter] = inventory; counter++; } } else { cout << "\nerror, unable open products.txt.\n"; errors = false; return errors; } }// ends readinventory double checkout() { double total = 0; //total cost string plucode; // code bool found = false; // notify if item found double costs; double quantity; bool codevalid = true; { cout << "please enter plu code or 0 exit: "; codevalid = true; cin >> plucode; /* (int x = 0; x <= plucode.length(); x++) { if ((plucode[x] != '1') && (plucode[x] != '2') && (plucode[x] != '3') && (plucode[x] != '4') && (plucode[x] != '5') && (plucode[x] != '6') && (plucode[x] != '7') && (plucode[x] != '8') && (plucode[x] != '9') && (plucode[x] != '0')) { codevalid = false; } } */ (int itemfind = 0 ; itemfind < counter; itemfind++) { if (itemarray[itemfind]->plu == plucode) { found = true; costs = itemarray[itemfind]->price; cout << "please enter lbs or quantity: "; cin >> quantity; if (costs); // data validation costs = quantity*costs; total += costs; }; }; if(!found) { cout << "please enter valid plu code.\n"; } } while (plucode != "0"); if (total > 50.00) total = (.95*total); return total; return 0.00; }//ends checkout bool updateinventory(string filename) { return true; }//ends updateinventory
here link actual assignment if needs it: https://www.dropbox.com/s/howsf5af2gsa1i8/cs1asg4bstructures.docx
to read lines file use following code:
std::string line; while (std::getline(inputfile,line)) { std::istringstream iss(line); product *inventory = new product(); iss >> inventory->plu; iss >> inventory->itemname; iss >> inventory->saletype; iss >> inventory->price; iss >> inventory->currentinventory; itemarray[counter] = inventory; ++counter; }
also try rid of global variable definitions (e.g. counter
, inputfile
) , provide them function local variables or parameters (or in case counter
reasonable use return value: return 0
or -1
error cases, , number of records read otherwise). you'll need check counter
less sizes
before allocating new record , assign itemarray[counter]
.
i recommend @ least using std::vector
manage records:
std::vector<product> itemarray; // ... std::string line; while (std::getline(inputfile,line)) { std::istringstream iss(line); product newprod; iss >> newprod.plu; iss >> newprod.itemname; iss >> newprod.saletype; iss >> newprod.price; iss >> newprod.currentinventory; itemarray.push_back(newprod); }
no need counter
code, itemarray.size()
tell number of records read file.
Comments
Post a Comment