Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- #define length 4
- using namespace std;
- class NotANumber { };
- int i = 0;
- class Arr
- {
- private:
- int arr[length];
- int *temp = arr;
- public:
- Arr() : arr()
- {}
- Arr(int *a)
- {
- for (int i = 0; i < length; i++)
- {
- arr[i] = a[i];
- }
- }
- int getElement(int i)
- {
- return arr[i];
- }
- int* set()
- {
- for (int i = 0; i < length; i++)
- cin >> arr[i];
- return arr;
- }
- int FindEl(int a)
- {
- for (int i = 0; i < length; i++) {
- cout << arr[i] << endl;
- if (arr[i] == a)
- {
- return i;
- }
- }
- cout << "No such element";
- }
- void PrintArray()
- {
- for (int j = 0; j < length; j++)
- cout << "arr[" << j << "]=" << arr[j] << endl;
- }
- friend ostream& operator << (ostream& os, Arr& a)
- {
- for (int i = 0; i < length; i++)
- os << a.arr[i] << " ";
- os << endl;
- return os;
- }
- friend istream& operator >> (istream& is, Arr& a)
- {
- for(int i=0;i < length;i++)
- is >>a.arr[i] ;
- return is;
- }
- };
- int main()
- {
- char ch;
- Arr a;
- ofstream ofile;
- ofile.open("array.dat");
- cout << "Enter array" << endl;
- try
- {
- cin >> a;
- ofile << a;
- }
- catch (NotANumber)
- {
- cin.clear();
- cin.sync();
- cout << "Something wrong";
- }
- ofile.close();
- ifstream ifile;
- ifile.open("array.dat");
- cout << "\nData in file:\n";
- try
- {
- ifile >> a;
- }
- catch (NotANumber)
- {
- cout << "Something wrong";
- }
- cout << a << endl;
- ifile.close();
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement