Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. // project3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8. using namespace std;
  9.  
  10. char fileName; // Windows name of the file in step 1
  11. char newFileName; // Windows file name of the file of extracted records
  12. int requestId; // product ID number used to extract data
  13. const int ORDER_VALUE = 500; // quantity value used to extract data, global const of value 500
  14. long idArray[20]; // array of ID numbers
  15. int storeArray[20]; // array of stored numbers
  16. int qtyArray[20]; // array of quantities
  17. int actualcount; // actual number of cells filled in the arrays
  18. int newcount; // number of extracted records written to newFileName
  19. int maxCells;
  20.  
  21. bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int &count, int maxCells);
  22. void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtyArray[], int count);
  23. bool extractData(const char newFileName[], int requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int& newcount);
  24.  
  25. int main()
  26. {
  27.     actualcount = 0;
  28.  
  29.     loadArrays("data.txt", idArray, storeArray, qtyArray, actualcount, 20);
  30.     printArrays(cout, idArray, storeArray, qtyArray, actualcount);
  31.     extractData("extractedata.txt", 96605, ORDER_VALUE, idArray, storeArray, qtyArray, actualcount, newcount);
  32.  
  33.     return 0;
  34. }
  35.  
  36. /*
  37. 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.
  38. Return true if you were able to load all of the data,
  39. return false otherwise.  The variable count will hold the
  40. number of cells loaded.*/
  41.  
  42. bool loadArrays(const char* fileName, long idArray[], int storeArray[], int qtyArray[], int &count, int maxCells)
  43. {
  44.     count = 0;
  45.     ifstream myfile(fileName);
  46.     if (myfile.is_open())
  47.     {
  48.         while (count < maxCells)
  49.         {
  50.             myfile >> idArray[count];
  51.             myfile >> storeArray[count];
  52.             myfile >> qtyArray[count];
  53.             count++;
  54.         }
  55.         if (count >= maxCells)
  56.         {
  57.             cout << "Unable to load all data, partial list below." << endl;
  58.         }
  59.         myfile.close();
  60.         return true;
  61.     }
  62.     else
  63.     {
  64.         cout << "File cannot be opened." << endl;
  65.         myfile.close();
  66.         return false;
  67.     }
  68. }
  69.  
  70.  
  71.  
  72. /*Will print to the stream where the data in the arrays.
  73. Count tells the number of cells filled.*/
  74.  
  75. void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtyArray[], int count)
  76. {
  77.     for (int x = 0; x < count; x++)
  78.     {
  79.         cout << "ID:" << idArray[x] << " Store Number: " << storeArray[x] << " Quantity: " << qtyArray[x] << endl;
  80.     }
  81. }
  82.  
  83.  
  84.  
  85. /*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
  86. file could not be opened, otherwise the return value will be true.*/
  87.  
  88.  
  89. bool extractData(const char* newFileName, int requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int &newcount)
  90. {
  91.     std::ofstream fileOut(newFileName);
  92.     newcount = 0;
  93.     if ((fileOut.is_open()))
  94.     {
  95.         for (int x = 0; x < count; x++)
  96.         {
  97.             if ((idArray[x] == requestId) && (qtyArray[x] < ORDER_VALUE))
  98.             {
  99.                 fileOut << "ID: " << idArray[x] << " quantity: " << qtyArray[x] << endl;
  100.                 newcount++;
  101.             }
  102.         }
  103.         fileOut.close();
  104.         return true;
  105.     }
  106.     else
  107.         return false;
  108. }
  109.  
  110. /*
  111. ID:16724 Store Number: 27 Quantity: 134
  112. ID:53602 Store Number: 83 Quantity: 233
  113. ID:75840 Store Number: 32 Quantity: 683
  114. ID:80833 Store Number: 75 Quantity: 135
  115. ID:69352 Store Number: 92 Quantity: 907
  116. ID:48582 Store Number: 34 Quantity: 625
  117. ID:76600 Store Number: 66 Quantity: 484
  118. ID:31592 Store Number: 35 Quantity: 400
  119. ID:96605 Store Number: 90 Quantity: 321
  120. ID:84393 Store Number: 67 Quantity: 539
  121. ID:46977 Store Number: 96 Quantity: 322
  122. ID:85737 Store Number: 49 Quantity: 430
  123. ID:32502 Store Number: 26 Quantity: 280
  124. ID:78712 Store Number: 75 Quantity: 789
  125. ID:76810 Store Number: 64 Quantity: 521
  126. ID:96605 Store Number: 51 Quantity: 114
  127. ID:57203 Store Number: 46 Quantity: 922
  128. ID:10937 Store Number: 12 Quantity: 435
  129. ID:37371 Store Number: 52 Quantity: 833
  130. ID:96605 Store Number: 26 Quantity: 577
  131. Press any key to continue . . .*/
  132.  
  133. /*
  134. Unable to load all data, partial list below.
  135. ID:16724 Store Number: 27 Quantity: 134
  136. ID:53602 Store Number: 83 Quantity: 233
  137. ID:75840 Store Number: 32 Quantity: 683
  138. ID:80833 Store Number: 75 Quantity: 135
  139. ID:69352 Store Number: 92 Quantity: 907
  140. ID:48582 Store Number: 34 Quantity: 625
  141. ID:76600 Store Number: 66 Quantity: 484
  142. ID:31592 Store Number: 35 Quantity: 400
  143. ID:96605 Store Number: 90 Quantity: 321
  144. ID:84393 Store Number: 67 Quantity: 539
  145. ID:46977 Store Number: 96 Quantity: 322
  146. ID:85737 Store Number: 49 Quantity: 430
  147. ID:32502 Store Number: 26 Quantity: 280
  148. ID:78712 Store Number: 75 Quantity: 789
  149. ID:76810 Store Number: 64 Quantity: 521
  150. ID:96605 Store Number: 51 Quantity: 114
  151. ID:57203 Store Number: 46 Quantity: 922
  152. ID:10937 Store Number: 12 Quantity: 435
  153. ID:37371 Store Number: 52 Quantity: 833
  154. ID:96605 Store Number: 26 Quantity: 577
  155. Press any key to continue . . .*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement