Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #define length 4
  6.  
  7. using namespace std;
  8.  
  9. class NotANumber { };
  10. int i = 0;
  11. class Arr
  12. {
  13. private:
  14. int arr[length];
  15. int *temp = arr;
  16. public:
  17. Arr() : arr()
  18. {}
  19. Arr(int *a)
  20. {
  21. for (int i = 0; i < length; i++)
  22. {
  23. arr[i] = a[i];
  24. }
  25. }
  26. int getElement(int i)
  27. {
  28. return arr[i];
  29. }
  30. int* set()
  31. {
  32. for (int i = 0; i < length; i++)
  33. cin >> arr[i];
  34. return arr;
  35. }
  36. int FindEl(int a)
  37. {
  38. for (int i = 0; i < length; i++) {
  39. cout << arr[i] << endl;
  40. if (arr[i] == a)
  41. {
  42. return i;
  43. }
  44. }
  45. cout << "No such element";
  46. }
  47.  
  48. void PrintArray()
  49. {
  50. for (int j = 0; j < length; j++)
  51. cout << "arr[" << j << "]=" << arr[j] << endl;
  52. }
  53.  
  54. friend ostream& operator << (ostream& os, Arr& a)
  55. {
  56. for (int i = 0; i < length; i++)
  57. os << a.arr[i] << " ";
  58. os << endl;
  59. return os;
  60. }
  61. friend istream& operator >> (istream& is, Arr& a)
  62. {
  63. for(int i=0;i < length;i++)
  64. is >>a.arr[i] ;
  65. return is;
  66. }
  67. };
  68.  
  69. int main()
  70. {
  71. char ch;
  72. Arr a;
  73. ofstream ofile;
  74. ofile.open("array.dat");
  75.  
  76. cout << "Enter array" << endl;
  77. try
  78. {
  79. cin >> a;
  80. ofile << a;
  81. }
  82. catch (NotANumber)
  83. {
  84. cin.clear();
  85. cin.sync();
  86. cout << "Something wrong";
  87. }
  88. ofile.close();
  89.  
  90. ifstream ifile;
  91. ifile.open("array.dat");
  92. cout << "\nData in file:\n";
  93.  
  94. try
  95. {
  96. ifile >> a;
  97. }
  98. catch (NotANumber)
  99. {
  100. cout << "Something wrong";
  101. }
  102. cout << a << endl;
  103. ifile.close();
  104. getchar();
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement