#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function prototypes.
int GetMaster (string[], string[], string[], double[], int);
bool GetInventory(const string[], int[], int);
int GetLocation(string, const string[], int);
bool CreateInventoryReport(const string[], const string[], const string[], const double[],
const int[], int);
double CalcValueOnHand(const double[],const int[], int);
// Global declarations.
const int MAXITEM = 100;
ifstream fin;
ofstream fout;
int main()
{
// Variables & declarations.
string category[MAXITEM];
string itemName [MAXITEM];
string code [MAXITEM];
double cost [MAXITEM];
int inventory[MAXITEM];
string itemCode;
int maxproducts;
int index;
maxproducts = GetMaster(category, itemName, code, cost, maxproducts);
GetInventory(code, inventory, maxproducts);
CreateInventoryReport(category, itemName, code, cost, inventory, maxproducts);
}
int GetMaster(string category[], string itemName[], string code[], double cost[], int maxproducts)
{
int counter = 0;
fin.open("CWC_Master.txt");
if(fin)
{
while(!fin.eof())
{
getline(fin, category[counter]);
getline(fin, itemName[counter]);
getline(fin, code[counter]);
fin >> cost[counter];
fin.ignore();
maxproducts = counter;
counter++;
}
fin.close();
return maxproducts;
}
else
{
cout << "Error! \"CWC_Master.txt\" was not found!\n";
return -1;
}
}
bool GetInventory(const string code[], int inventory[], int maxproducts)
{
fin.open("scan_inv.txt");
string itemCode;
int index;
if(fin)
{
for(int y = 0; y < maxproducts; y++)
{
index = GetLocation(itemCode, code, maxproducts);
fin >> inventory[index];
}
fin.close();
return true;
}
else
{
cout << "Error! \"scan_inv.txt\" was not found!\n";
return false;
}
}
int GetLocation(string itemCode, const string code[], int maxproducts)
{
fin >> itemCode;
for(int x = 0; x < maxproducts; x++)
{
if(itemCode == code[x])
{
return x;
}
}
}
bool CreateInventoryReport(const string category[], const string itemName[],
const string code[], const double cost[], const int inventory[],
int maxproducts)
{
fout.open("prog8_out_.txt");
if(fout)
{
int sum;
sum = CalcValueOnHand(cost, inventory, maxproducts);
fout << setprecision(3);
fout << setw(50) << "Cubicle Widgets Inventory\n";
fout << category[0] << endl; for(int z = 0; z < maxproducts; z++)
{
if(category[0] == category[z])
{
fout << "Item Code\t\t" << "Cost\t\t" << "Inventory\t\t" << "Item Name\n";
fout << code[z] << "\t\t$" << cost[z] << "\t\t" << inventory[z] << "\t\t" << itemName[z] << endl << endl;
}
else if (category[z] != category[z+1])
{
fout << category[0] << endl;
fout << "Item Code\t\t" << "Cost\t\t" << "Inventory\t\t" << "Item Name\n\n";
}
else if (category[z] == category[z+1])
{
fout << code[z] << "\t\t$" << cost[z] << "\t\t" << inventory[z] << "\t\t" << itemName[z];
}
}
}
}
double CalcValueOnHand(const double cost[], const int inventory[], int maxproducts)
{
int sum = 0;
for(int i = 0; i < maxproducts; i++)
{
sum += (cost[i]*inventory[i]);
}
return sum;
}