Advertisement
mostlabs

15/1

May 15th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5.  
  6. using namespace std;
  7. void siralama(struct al* a);
  8. void gir(struct al* a);
  9. void yaz(struct al* a);
  10.  
  11. struct al
  12. {
  13.     string country;
  14.     string city;
  15.  
  16. };
  17. void gir(struct al* a) {
  18.  
  19.     for (int i = 0; i < 5; i++) {
  20.         cout << "country" << i + 1 << ":";
  21.         cin >> a[i].country;
  22.         cout << "city" << i + 1 << ":";
  23.         cin >> a[i].city;
  24.  
  25.     }
  26.  
  27. }
  28. void yaz(struct al* a) {
  29.  
  30.     ofstream yap("myfile.txt", ios::binary);
  31.     for (int i = 0; i < 5; i++) {
  32.  
  33.         yap << a[i].country << "-" << a[i].city << endl;
  34.     }
  35.  
  36.     yap.close();
  37.  
  38. }
  39. void siralama(struct al* a) {
  40.  
  41.     ofstream ayir("deneme2.txt", ios::binary);
  42.  
  43.     for (int i = 0; i < 5; i++) {
  44.  
  45.         if ((*(a + i)).country == "italy") {
  46.  
  47.             ayir << a[i].country << "-" << a[i].city << endl;
  48.         }
  49.  
  50.  
  51.     }
  52.     ayir.close();
  53. }
  54. int main()
  55. {
  56.  
  57.     ifstream fromfile;
  58.  
  59.    
  60.     struct al a[100];
  61.     string satir = " ";
  62.     char b = 'n';
  63.  
  64.     do {
  65.         cout << "select a number:" << endl;
  66.         cout << "1-data entry:" << endl << "2-print and reading the entered data to a file:" << endl << "3:Sorting data:" << endl << "4:delete data:" << endl;
  67.         int task;
  68.  
  69.         cin >> task;
  70.  
  71.         cin.get();
  72.         switch (task)
  73.         {
  74.  
  75.         case 1: {
  76.             gir(&a[0]);
  77.  
  78.             break;
  79.  
  80.         }
  81.         case 2: {
  82.             ofstream yap("myfile.txt", ios::binary);
  83.             yaz(&a[0]);
  84.             fromfile.open("myfile.txt", ios::binary);
  85.  
  86.             while (getline(fromfile, satir)) {
  87.  
  88.                 cout << satir << endl;
  89.  
  90.  
  91.             }
  92.  
  93.             fromfile.close();
  94.  
  95.  
  96.             yap.close();
  97.  
  98.             break;
  99.         }
  100.  
  101.         case 3: {
  102.  
  103.             ofstream ayir("deneme2.txt",ios::binary);
  104.             siralama(&a[0]);
  105.             ifstream bir("deneme2.txt", ios::binary);
  106.  
  107.  
  108.             while (getline(bir, satir)) {
  109.  
  110.                 cout << satir << endl;
  111.  
  112.  
  113.             }
  114.             bir.close();
  115.  
  116.             ayir.close();
  117.  
  118.             break;
  119.         }
  120.         case 4: {
  121.             int sonuc = remove("deneme2.txt");
  122.  
  123.             if (sonuc == 0) {
  124.  
  125.                 cout << "file deleted!!" << endl;;
  126.             }
  127.  
  128.             break;
  129.  
  130.         }
  131.         default:
  132.  
  133.         {
  134.  
  135.             cout << "Incorect task number" << "\n";
  136.  
  137.             break;
  138.  
  139.         }
  140.  
  141.  
  142.  
  143.  
  144.         }
  145.         cout << "If you want to exit, input y" << endl;
  146.  
  147.         cin >> b;
  148.  
  149.         cin.ignore(256, '\n');
  150.  
  151.  
  152.     } while (b != 'y'); {
  153.  
  154.  
  155.         cout << "exit!!";
  156.     }
  157.  
  158.  
  159.  
  160.  
  161.     return 0;
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement