Advertisement
Zeinab_Hamdy

Untitled

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