1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace::std;
const int ORDER_VALUE = 500;
bool loadArrays(const char[], long[], int[], int[], int &, int); // <--
void printArrays(ostream &, const long[], const int[], const int[], int); // <--
bool extractData(const char[], int, int, const long[], const int[], const int[], int, int&); // <--
int main()// <--
{
const int maxCells = 100;
const char fileName[100] = "Project3Records.txt"; // <--
const char newFileName[100] = "Project3ExtractedRecords.txt"; // <--
int requestId;
long idArray[maxCells];
int storeArray[maxCells];
int qtyArray[maxCells];
int count = 0;
int newCount = 0;
bool loadResult;
loadResult = loadArrays(fileName, idArray, storeArray, qtyArray, count, maxCells);
if (loadResult == false)
{
if (count == 0)
{
cout << "Could not open input file " << fileName << endl;
exit(1);
}
else
{
cout << "Could not load all data from: " << fileName << endl;
exit(2);
}
}
printArrays(cout, idArray, storeArray, qtyArray, count);
cout << "Please enter a product ID: ";// <--
cin >> requestId;
bool requestResult;
requestResult = extractData(
newFileName,
requestId,
ORDER_VALUE,
idArray,
storeArray,
qtyArray,
count,
newCount // <--
);
if (requestResult == false)
{
cout << "Could not open file: " << newFileName << endl;
exit(3);
}
cout << newCount << "lines were extracted." << endl;
return 0;
}
bool loadArrays(const char name[], long id[], int store[], int qty[], int &cnt, int max)
{
ifstream in;
in.open(name); // <---
if (!in)
{
cout << "Can not open file." << endl;
exit(8);
}
int i = 0;
while (i < max && in >> id[i] >> store[i] >> qty[i])
{
i++;
}
cnt = i;
if (in.good()) //still more data in file if good
{
in.close();
return false;
}
else
{
in.close();
return true;
}
}
void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtArray[], int count)
{
for (int i = 0; i < count; i++) //<---
{
where << idArray[i] << " " << storeArray[i] << " " << qtArray[i] << endl;
}
return;
}
bool extractData(const char newFile[], int reqId, int baseQty, const long idArray[], const int storeArray[], const int qtArray[], int count, int & newCount)
{
ofstream out;
out.open(newFile);
if (!out) return false;
newCount = 0;
for (int i = 0; i < count; i++) // <--
{
if (qtArray[i] < baseQty && reqId ???????? ) // <-- Fill in
{
out << reqId << ' ' << idArray[i] ????????? << '\n'; // <-- Fill in
newCount++;
}
}
out.close(); // <--
return true;
}
|