Advertisement
CHU2

Fstream

Mar 12th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void classmates();
  8. void add();
  9. void update();
  10. void delet();
  11.  
  12. int main() {
  13.     int option;
  14.    
  15.     classmates();
  16.  
  17.     cout << "---------------------------------------- \n";
  18.     cout << "   CLASSMATE NAMES \n\n choose options: ";
  19.     cout << "\n [1] add";
  20.     cout << "\n [2] update";
  21.     cout << "\n [3] delete";
  22.     cout << "\n Enter your option: ";
  23.     cin >> option;
  24.  
  25.     switch (option) {
  26.     case 1:
  27.         cout << "---------------------------------------- \n";
  28.         cout << " ADD NAMES \n\n";
  29.        
  30.         add();
  31.         classmates();
  32.         break;
  33.     case 2:
  34.         cout << "---------------------------------------- \n";
  35.         cout << " UPDATE NAMES \n\n";
  36.  
  37.         update();
  38.         classmates();
  39.         break;
  40.     case 3:
  41.         cout << "---------------------------------------- \n";
  42.         cout << " DELETE NAMES \n\n";
  43.  
  44.         delet();
  45.         classmates();
  46.         break;
  47.     default:
  48.         cout << "Input is Invalid. Mismatch. \n";
  49.     }
  50.     return 0;
  51. }
  52. void add(){
  53.     string line;
  54.     fstream tex;
  55.  
  56.     cout << "Enter New Classmates: ";
  57.     cin >> line;
  58.  
  59.     tex.open("tex.txt", ios::app);
  60.     tex << "\n" << line;
  61.     tex.close();
  62. }
  63.  
  64. void classmates() {
  65.     string line;
  66.     fstream classmates;
  67.  
  68.     classmates.open("tex.txt");
  69.     while (getline(classmates, line)) {
  70.         cout << line << "\n";
  71.     }
  72.     classmates.close();
  73. }
  74.  
  75. void update() {
  76.     string line, up, rep;
  77.     fstream classmates;
  78.  
  79.     cout << "Find: ";
  80.     cin >> up;
  81.     cout << "Replace: ";
  82.     cin >> rep;
  83.  
  84.     classmates.open("tex.txt", ios::in);
  85.     while (getline(classmates, line)) {
  86.         if (line.find(up)) {
  87.             cout << "\n" << rep;
  88.         }
  89.     }
  90.     classmates.close();
  91. }
  92.  
  93. void delet() {
  94.     string line, up, rep;
  95.     fstream classmates;
  96.  
  97.     classmates.open("tex.txt", ios::out);
  98.     while (getline(classmates, line)) {
  99.             cout << "\n";
  100.     }
  101.     classmates.close();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement