Advertisement
Zeinab_Hamdy

Untitled

Mar 15th, 2023
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int sz= 101;
  4. bool visited[sz];
  5.  
  6.  
  7. class Customer
  8. {
  9.  
  10.     int id ;
  11.     char name[20];
  12.  
  13. public :
  14.  
  15.     bool searchId(int val)
  16.     {
  17.         ifstream in ("data.txt", ios:: in);
  18.  
  19.         if(in.is_open())
  20.         {
  21.             Customer c1;
  22.  
  23.             while(!in.eof())
  24.             {
  25.                 in.read( (char *)& c1, sizeof(c1));
  26.                 if(c1.id== val) return true;
  27.             }
  28.  
  29.         }
  30.         else cout << "Cann't open the file ";
  31.         in.close();
  32.  
  33.         return false;
  34.     }
  35.  
  36.     bool validId( double val){
  37.  
  38.        return ( val > 0 and int(val)== val  and (!searchId(val) or !visited[int(val)] )) ;
  39.      }
  40.  
  41.  
  42.     void setId()
  43.     {
  44.         double x ;
  45.         cout << "Enter ID : ";
  46.         cin >> x;
  47.  
  48.  
  49.         while(!validId(x) )
  50.         {
  51.             cout <<"Invalid Id , please try again \n";
  52.             cout << "Enter ID : ";
  53.             cin >> x;
  54.  
  55.         }
  56.         id = int(x);
  57.         visited[id]= true;
  58.  
  59.     }
  60.  
  61.     int getId()
  62.     {
  63.         return id;
  64.     }
  65.  
  66.     void setName()
  67.     {
  68.         cout << "Enter name : ";
  69.         cin >> name ;
  70.     }
  71.  
  72.     char* getName()
  73.     {
  74.         return name;
  75.     }
  76. };
  77.  
  78.  
  79. int main()
  80. {
  81.  
  82. for(int i=0 ; i<sz ;i++) visited[i]= false;
  83.  
  84.     Customer c1 ;
  85.     ofstream out("data.txt", ios::out | ios::app );
  86.     char ch='Y';
  87.     do{
  88.         c1.setId();
  89.             visited[c1.getId()] = true;
  90.         c1.setName();
  91.         out.write( (char *)&c1 , sizeof(c1));
  92.         cout <<"press Y to continue";
  93.         cin >> ch ;
  94.  
  95.     }while(ch=='Y');
  96.  
  97.     out.close();
  98.  
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement