i need some help with a program. it is fairly large, so i will just attach a zip file with the source and input/output files. i just started to learn arrays and i am a little confused. don't worry about how the output is formatted. i can fix that later. i just need help with the arrays and functions.
Code:#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; long isInInventory(string,string[],long); // Parameters: // codeToFind: product code being searched for // code: product codes in inventory // count: number of product codes in inventory // Discussion: // If a product with a code matching the parameter codeToFind is found in the array // code then the index of this code is returned, otherwise -1 is returned long readInventory(string,string[],string[],double[]); // Parameters: // storeName: name of store // code: product codes in inventory // description: product descriptions in inventory // unitPrice: unit prices in inventory // Discussion: // Reads data from inventory file into arrays above. // Returns number of products actually read // Data file is formatted as follows: // first line: name of the store (at most 40 characters) // even-numbered lines: five-character product code followed by product description // other odd-numbered lines: unit price of product described in above line int main() { // Programmer: Mike Gozdiskowski // Date: 11/13/2002 // Discussion: Allow user to search inventory of a small store. // // Input: User must enter product codes until a sentinel value is entered // Output: If user enters a product code that corresponds to the inventory // all of the information for that product code will be displayed // on the screen and also printed to an output log file for future // reference. If the user enters a bad product code; they are // informed on the screen and also in the output file. //Declaration & description of variables string storeName; //Store name string productCode; //Code entered by user string A[30]; //Array to store values for code string B[30]; //Array to store values for description double C[30]; //Array to store values for price long count; //Number of positions of array A with meaningful values long index; //Array position for array //Read data from file count=readInventory(storeName,A,B,C); //Print heading to log file program5.out ofstream fout("program5.out"); fout<<storeName; fout<<"Product Code Description Unit Price"<<endl; fout<<"************ ************************* **********"<<endl; //Welcome user to the program & ask for product code cout<<"Welcome to "<<storeName<<endl; cout<<"Please enter a product code (Q to Quit): "; cin>>productCode; //Loop until sentinel is entered while(productCode!="Q") { //Use a function to search the product code array for this code index=isInInventory(productCode,A,count); //If the product code is found print information to the log file //and the user screen otherwise print the product code and the error //message to both the log file and the user screen if(index>=0) { cout<<"Product Code: "<<A<<endl; cout<<"Description: "<<B<<endl; cout<<"Unit Price: "<<C<<endl; fout<<setw(5)<<A<<setw(25)<<B<<setw(40-(A[].length()+B[].length()))C[]<<endl; } else { cout<<productCode<<"ERROR: Invalid product code"<<endl; fout<<productCode<<"ERROR: Invalid product code"<<endl; } //Ask user for next product code cout<<"Next product code (Q to Quit): "; cin>>productCode; } //Finish report and say goodbye to user fout<<"End-of-Report"<<endl; cout<<"Program terminated check program5.out for results"<<endl; return 0; }//main long isInInventory(string codeToFind,string code[],long count) { //Searches array for number; return index if found, otherwise returns -1 long index; //Array index //Loop: search positions 0 through count-1 of array codeToFind for(index=0;index<count;index++) //Is this the code we are looking for? if(codeToFind[index]==code) return code; //Searched entire array without finding code return -1; //Failed to find code }//isInInventory long readInventory(string &storeName,string code[],string description[],double unitPrice[]) { //Reads data from file into arrays string x; //Code value read from file string y; //Description value read from file double z; //UnitPrice read from file long codeCount, descCount, priceCount; //Counters for arrays ifstream fin("program5.txt"); //Data file //Initialize number of values and get values from file codeCount=0; descCount=0; priceCount=0; fin>>x>>y>>z; //Get store name getline(fin, storeName); //Loop: continue until eof has been reached while(!fin.eof()) { //Store value read in array code and update counter code[codeCount]=x; codeCount++; //Store value in array description and update counter description[descCount]=y; descCount++; //Store value in array unitPrice and update counter unitPrice[priceCount]=z; priceCount++; //Read next values from file fin>>x>>y>>z; }//While //Return number of values read return codeCount, descCount, priceCount; }//ReadInventory



LinkBack URL
About LinkBacks


