Advertisement
Zeinab_Hamdy

Untitled

Mar 15th, 2023
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Customer{
  5.  
  6. // data members --- by default private
  7.  int id ;
  8.  char name[20];
  9.  
  10. // funtcion
  11.  
  12.  public :
  13.  
  14.      void setId(){
  15.          cout <<"Enter ID : ";
  16.          cin >> id;
  17.      }
  18.  
  19.      int getId(){
  20.         return id;
  21.      }
  22.  
  23.      void setName(){
  24.         cout << "Enter name : ";
  25.         cin >> name ;
  26.      }
  27.  
  28.      char* getName(){
  29.        return name;
  30.      }
  31. };
  32.  
  33.  
  34. int main(){
  35.  
  36.   ofstream out("data.txt" , ios::out | ios::app );
  37.   Customer c1 ;
  38.   char  ch = 'Y';
  39.   do{
  40.     c1.setId();
  41.     c1.setName();
  42.  
  43.     out.write( (char*)&c1 , sizeof(c1));
  44.     cout <<"if you want to enter another data \t press Y/N \n";
  45.     cin >> ch ;
  46.   }while( ch=='Y');
  47.  
  48.   out.close();
  49.  
  50.  
  51.   ifstream in ("data.txt" , ios::in);
  52.   if(in.is_open()){
  53.     cout << "Id\tName\n";
  54.     while(!in.eof()){
  55.         in.read( (char *) &c1 , sizeof(c1));
  56.         if(!in.eof())
  57.         cout << c1.getId() << '\t' << c1.getName() << "\n" ;
  58.     }
  59.   }else cout <<"Can't open the file \n";
  60.   in.close();
  61.  
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement