Inventory Struct Array Lookup for Purchases Create the console program described
ID: 3730637 • Letter: I
Question
Inventory Struct Array Lookup for Purchases Create the console program described in a suitable folder. Name the Project Assign01-InventoryStruct Copy the two input data files -Inventory.txt and Purchases.txt to the project folder for processing array of record structs to hold the four inventory file values used to lookup data and process each purchase Suggested variables and declarations: #include #include #include #include using namespace std; struct Inventory.txt- Notepad Phoenix Juicer AX205 114.95 18 BL114 149.95 42 Harper_ Sound Monitor BY684 469.95 8 CL120 27.49 345 CL455 69.95 201 DD889 14.25 893 DQ114 18.49 81 Lerner Multi-Tiner EG802 34.95 22 Zygran Humidifier FA105 7.95 137 olden Video Splitter FZ225 12.49 73 Yard-eze CarryAll Feld Convection Oven Holden DoorGuard Entrex Static Hat Evans FloorCote inventoryType string itemCode; double itemPrice; int string itemName; itemOnHand; inventoryType void inventoryLookup(inventoryType inv[], int cnt, string code, int qty); inventoryReport(inventoryType inv[]. int cnt); int main() inventoryType inventoryRec[20]; array of Inventory records 361 0114 inventoryType inventoryMatch; CL120 FA105 FZ225 BY684 BL114 215 12 21 1 362 / matched Inventory record itemCount purcha secount purchaseAmount; // count of Inventory records364 // count of Purchases records 365 6; calculated amount total int char dollarsign ='5; string purchaseNum; string purchaseItem; int E6882 16 367 368 369 //Purchases file field values DQ114 18 purchasety; 371 372 D0559 325 CL455 78 Inventory input file Purchases input file AX205 ifstream inventoryFile; ifstream purchasesFile; 374 375 FA105 43 BY6842 Open the two input files and test both input streams. Issue an error message 6 3 and return -I to exit the main routine if either input file open attempt fails.377 09 208 74 Load each of four data items in an inventory record into an Inventory that forms a single entry in an array of structs (inventory ReelI). Count the 38 number of items loaded into this array and save this value (itemCount) in record 378 FA185 order to control loops that will process and search the array in called routines (inventory LookupO) and inventory ReportO)). Close the Inventory input file. Display the count of inventory records (itemCount). Set up a while loop to process each record until end-of file. Read the first purchase record into three variables. (Suggestion: Use a priming read to input the first field of the first record into the purchaseNum field on the line before the start of the while loop. Stream the other two purchases record fields into the purchaseltem and purchaseOty fields at Within the while loop (after reading each entire purchase record) . Lookup and return a matched Inventory record (inventoryMatch) based on the matching 2. Calculate total purchase amount for each purchase, multiplying matched item price bypurchase quan the top of the loop. Repeat priming read at the very bottom of loop.) input itemCode Display complete Purchase information in a stream to cout, including matched invent 4. Increment the purchase count variable to track the number of Purchase recordsExplanation / Answer
An array of structure can be made just like creating any other type of array.
syntax: structurename arrayname[size];
This creates an array of structures with given size.
Each element of the structure can accessed by using the dot(.) operator
arrayname[index].elementname
//CODE STARTS HERE
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip> //for setw(),fixed,setprecision()
using namespace std;
struct inventoryType
{
string itemCode;
double itemPrice;
int itemOnHand;
string itemName;
};
inventoryType inventoryLookup(inventoryType inv[],int cnt,string code,int qty)
{
for(int i=0;i<cnt;i++)
{
if(inv[i].itemCode.compare(code)==0)
{
inv[i].itemOnHand-=qty;
return inv[i];
}
}
cout<<"Not found ";
return inv[0];
}
void inventoryReport(inventoryType inv[],int cnt)
{
cout<<" items on hand: ";
cout<<fixed<<setprecision(2);
cout<<inv[0].itemCode<<" $"<<setw(8)<<inv[0].itemPrice<<setw(7)<<inv[0].itemOnHand<<" "<<inv[0].itemName<<" ";
for(int i=1;i<cnt;i++)
cout<<inv[i].itemCode<<" "<<setw(8)<<inv[i].itemPrice<<setw(7)<<inv[i].itemOnHand<<" "<<inv[i].itemName<<" ";
return ;
}
int main()
{
inventoryType inventoryRec[20];
inventoryType inventoryMatch;
int itemCount=0;
int purchaseCount=0;
double purchaseAmount;
char dollarSign='$';
string purchaseNum;
string purchaseItem;
int purchaseQty;
ifstream inventoryFile("Inventory.txt");
if(inventoryFile.fail())
{
cout<<"Inventory.txt not found";
return -1;
}
ifstream purchasesFile("Purchases.txt");
if(purchasesFile.fail())
{
cout<<"Purchases.txt not found";
return -1;
}
while(!inventoryFile.eof())
{
string titemcode; //temporary variables
double titemprice;
int titemonhand;
string titemname;
inventoryFile>>titemcode>>titemprice>>titemonhand>>titemname; //Taking input from Inventory.txt
inventoryRec[itemCount].itemCode=titemcode;
inventoryRec[itemCount].itemPrice=titemprice;
inventoryRec[itemCount].itemOnHand=titemonhand;
inventoryRec[itemCount].itemName=titemname;
itemCount++;
}
inventoryFile.close();
cout<<" "<<itemCount<<" items loaded from Inventory purchases: ";
purchasesFile>>purchaseNum; // Taking input from Purchases.txt
while(!purchasesFile.eof())
{
purchasesFile>>purchaseItem>>purchaseQty;
purchaseCount++;
inventoryMatch=inventoryLookup(inventoryRec,itemCount,purchaseItem,purchaseQty);
purchaseAmount=(inventoryMatch.itemPrice)*purchaseQty;
cout<<fixed<<setprecision(2);
cout<<purchaseNum<<" "<<purchaseItem<<setw(7)<<purchaseQty<<" @ "<<dollarSign<<setw(7)<<inventoryMatch.itemPrice<<" "<<dollarSign<<setw(10)<<purchaseAmount<<" "<<inventoryMatch.itemName<<" ";
if(purchaseCount==1)
dollarSign=' ';
purchasesFile>>purchaseNum;
}
cout<<" "<<purchaseCount<<" items purchased. ";
inventoryReport(inventoryRec,itemCount);
purchasesFile.close();
return 0;
}
//If you found this helpful, please leave a like
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.