Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // project3.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- using namespace std;
- char fileName; // Windows name of the file in step 1
- char newFileName; // Windows file name of the file of extracted records
- int requestId; // product ID number used to extract data
- const int ORDER_VALUE = 500; // quantity value used to extract data, global const of value 500
- long idArray[20]; // array of ID numbers
- int storeArray[20]; // array of stored numbers
- int qtyArray[20]; // array of quantities
- int actualcount; // actual number of cells filled in the arrays
- int newcount; // number of extracted records written to newFileName
- int maxCells;
- bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int &count, int maxCells);
- void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtyArray[], int count);
- bool extractData(const char newFileName[], int requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int& newcount);
- int main()
- {
- actualcount = 0;
- loadArrays("data.txt", idArray, storeArray, qtyArray, actualcount, 20);
- printArrays(cout, idArray, storeArray, qtyArray, actualcount);
- extractData("extractedata.txt", 96605, ORDER_VALUE, idArray, storeArray, qtyArray, actualcount, newcount);
- return 0;
- }
- /*
- Will load the three arrays from data from the disk file and use count for the number of cells loaded. Be certain to check that you do not load more cells than you have dimensioned.
- Return true if you were able to load all of the data,
- return false otherwise. The variable count will hold the
- number of cells loaded.*/
- bool loadArrays(const char* fileName, long idArray[], int storeArray[], int qtyArray[], int &count, int maxCells)
- {
- count = 0;
- ifstream myfile(fileName);
- if (myfile.is_open())
- {
- while (count < maxCells)
- {
- myfile >> idArray[count];
- myfile >> storeArray[count];
- myfile >> qtyArray[count];
- count++;
- }
- if (count >= maxCells)
- {
- cout << "Unable to load all data, partial list below." << endl;
- }
- myfile.close();
- return true;
- }
- else
- {
- cout << "File cannot be opened." << endl;
- myfile.close();
- return false;
- }
- }
- /*Will print to the stream where the data in the arrays.
- Count tells the number of cells filled.*/
- void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtyArray[], int count)
- {
- for (int x = 0; x < count; x++)
- {
- cout << "ID:" << idArray[x] << " Store Number: " << storeArray[x] << " Quantity: " << qtyArray[x] << endl;
- }
- }
- /*Will use the value in requestId to write only the records with that id number that have a quantity below baseQty(the ORDER_VALUE) to the new file. The variable newcount will hold the number of records actually written. The return value will be false if the
- file could not be opened, otherwise the return value will be true.*/
- bool extractData(const char* newFileName, int requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int &newcount)
- {
- std::ofstream fileOut(newFileName);
- newcount = 0;
- if ((fileOut.is_open()))
- {
- for (int x = 0; x < count; x++)
- {
- if ((idArray[x] == requestId) && (qtyArray[x] < ORDER_VALUE))
- {
- fileOut << "ID: " << idArray[x] << " quantity: " << qtyArray[x] << endl;
- newcount++;
- }
- }
- fileOut.close();
- return true;
- }
- else
- return false;
- }
- /*
- ID:16724 Store Number: 27 Quantity: 134
- ID:53602 Store Number: 83 Quantity: 233
- ID:75840 Store Number: 32 Quantity: 683
- ID:80833 Store Number: 75 Quantity: 135
- ID:69352 Store Number: 92 Quantity: 907
- ID:48582 Store Number: 34 Quantity: 625
- ID:76600 Store Number: 66 Quantity: 484
- ID:31592 Store Number: 35 Quantity: 400
- ID:96605 Store Number: 90 Quantity: 321
- ID:84393 Store Number: 67 Quantity: 539
- ID:46977 Store Number: 96 Quantity: 322
- ID:85737 Store Number: 49 Quantity: 430
- ID:32502 Store Number: 26 Quantity: 280
- ID:78712 Store Number: 75 Quantity: 789
- ID:76810 Store Number: 64 Quantity: 521
- ID:96605 Store Number: 51 Quantity: 114
- ID:57203 Store Number: 46 Quantity: 922
- ID:10937 Store Number: 12 Quantity: 435
- ID:37371 Store Number: 52 Quantity: 833
- ID:96605 Store Number: 26 Quantity: 577
- Press any key to continue . . .*/
- /*
- Unable to load all data, partial list below.
- ID:16724 Store Number: 27 Quantity: 134
- ID:53602 Store Number: 83 Quantity: 233
- ID:75840 Store Number: 32 Quantity: 683
- ID:80833 Store Number: 75 Quantity: 135
- ID:69352 Store Number: 92 Quantity: 907
- ID:48582 Store Number: 34 Quantity: 625
- ID:76600 Store Number: 66 Quantity: 484
- ID:31592 Store Number: 35 Quantity: 400
- ID:96605 Store Number: 90 Quantity: 321
- ID:84393 Store Number: 67 Quantity: 539
- ID:46977 Store Number: 96 Quantity: 322
- ID:85737 Store Number: 49 Quantity: 430
- ID:32502 Store Number: 26 Quantity: 280
- ID:78712 Store Number: 75 Quantity: 789
- ID:76810 Store Number: 64 Quantity: 521
- ID:96605 Store Number: 51 Quantity: 114
- ID:57203 Store Number: 46 Quantity: 922
- ID:10937 Store Number: 12 Quantity: 435
- ID:37371 Store Number: 52 Quantity: 833
- ID:96605 Store Number: 26 Quantity: 577
- Press any key to continue . . .*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement