Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. // Specification file for the NuberArrayClass
  2. //a.k.a NumberArrayClass.h file
  3.  
  4. #ifndef NUMBERARRAYCLASS_H
  5. #define NUMBERARRAYCLASS_H
  6.  
  7. using namespace std;
  8. // class declaration
  9.  
  10. class NumberArrayClass
  11. {
  12.  
  13. private:
  14. int arraySize;
  15. double * numberArray = nullptr;
  16.  
  17. public:
  18. //constructor declaration
  19. NumberArrayClass();
  20.  
  21. //member functions
  22. void readDataFromFile();
  23. void displayArray();
  24.  
  25. //destructor
  26. ~NumberArrayClass()
  27. {
  28. delete[] numberArray;
  29. } // end of destructor
  30.  
  31.  
  32. };
  33. #endif // NUMBERARRAYCLASS_H
  34.  
  35. //NumberArrayClass.cpp file
  36.  
  37. #include "NumberArrayClass.h" //needed to access arry
  38. #include <iostream>
  39. #include <fstream> //needed for file read
  40.  
  41. using namespace std;
  42.  
  43.  
  44.  
  45.  
  46.  
  47. NumberArrayClass::NumberArrayClass()
  48. {
  49. arraySize = 250;
  50. numberArray = new double[arraySize];
  51. }
  52.  
  53.  
  54.  
  55. void NumberArrayClass::readDataFromFile()
  56. {
  57.  
  58. //creating a read object and opening file.
  59. ifstream inFile;
  60.  
  61. inFile.open("DoubleData.txt");
  62.  
  63. int countIt = 0; //this is a counter
  64.  
  65. if (!inFile.fail())
  66. {
  67. cout << "File open!" << endl;
  68. //this should populate the
  69. while (countIt < arraySize && inFile >> numberArray[countIt]);
  70. {
  71. countIt++; //incramenting counter.
  72. }
  73. }
  74. else
  75. {
  76. cout << "File Read fail!" << endl;
  77. }//end of if/else statement.
  78.  
  79.  
  80.  
  81. }//end of readDataFromFile
  82.  
  83.  
  84. void NumberArrayClass::displayArray()
  85. {
  86.  
  87. for (int i = 0; i < arraySize; i++)
  88. {
  89. cout << numberArray[i] << endl;
  90. }
  91.  
  92. }//end of displayArray
  93.  
  94. //main .cpp file
  95. #include <iostream>
  96. #include "NumberArrayClass.h"
  97.  
  98. using namespace std;
  99.  
  100. int main()
  101. {
  102. //creating our class object.
  103. NumberArrayClass array1;
  104.  
  105. array1.readDataFromFile();
  106.  
  107. array1.displayArray();
  108.  
  109. system("pause");
  110. return 0;
  111. }
  112.  
  113. while (... && inFile >> numberArray[countIt]);
  114.  
  115. numberArray = new double[arraySize];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement