Advertisement
CHU2

Fstream v2

May 21st, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.76 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <vector>       //not in my life would I use this;<
  9.  
  10. using namespace std;
  11.  
  12. struct info {
  13.     string lname;
  14.     string fname;
  15.     string mname;
  16.     int age;
  17.     string gender;
  18. };
  19.                         //table of contents
  20. void start();                                       //line 36
  21. void home();                                        //line 63
  22. void add();                                         //line 96
  23. void savedata(list <info>);                         //line 121
  24. void view();                                        //line 151
  25. void search();                                      //line 205
  26. void stringsearch(int, string);                     //line 262
  27. void edit(int, string, string);                     //line 327
  28. void deleto(int, string, string);                   //line 382
  29. void savedatav2(list <info>);                       //line 417
  30. void sort();                                        //line 447
  31. void stringsort(int, string);                       //line 504
  32. void stringquicksort(vector <info>&);
  33. void intquicksort(vector <info>&);
  34. void savedatav3(vector <info>);
  35.  
  36. int main() {
  37.     start();
  38.  
  39.     return 0;
  40. }
  41.  
  42. void start() {
  43.     char choice;
  44.  
  45.     cout << "\n-------------------------------------------------------------------------"
  46.          << "\nSTUDENT PROFILE INFORMATION SYSTEM"
  47.          << "\n[a] Start"
  48.          << "\n[b] End"
  49.          << "\nEnter your Choice: ";
  50.     cin >> choice;
  51.     system("cls");
  52.  
  53.     switch (choice) {
  54.     case 'a': {
  55.         cout << "\nLoad is Success, Match";
  56.         home();
  57.         break;
  58.     }
  59.     case 'b': {
  60.         break;
  61.     }
  62.     default: {
  63.         cout << "\nChoice is None, Mismatch";
  64.         start();
  65.     }
  66.     }
  67. }
  68.  
  69. void home() {
  70.     char choice;
  71.  
  72.     cout << "\n-------------------------------------------------------------------------"
  73.          << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Home"
  74.          << "\n[a] Add"
  75.          << "\n[b] View"
  76.          << "\n[c] End"
  77.          << "\nEnter your Choice: ";
  78.     cin >> choice;
  79.     system("cls");
  80.  
  81.     switch (choice) {
  82.     case 'a': {
  83.         cout << "\nLoad is Success, Match";
  84.         add();
  85.         break;
  86.     }
  87.     case 'b': {
  88.         cout << "\nLoad is Success, Match";
  89.         view();
  90.         break;
  91.     }
  92.     case 'c': {
  93.         break;
  94.     }
  95.     default: {
  96.         cout << "\nChoice is None, Mismatch";
  97.         home();
  98.     }
  99.     }
  100. }
  101.  
  102. void add() {
  103.     list <info> adding;
  104.     string name[4];
  105.     int age;
  106.  
  107.     cout << "\n-------------------------------------------------------------------------"
  108.         << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Add"
  109.         << "\nLast Name: ";
  110.     cin >> name[0];
  111.     cout << "\nFirst Name: ";
  112.     cin >> name[1];
  113.     cout << "\nMiddle Initial: ";
  114.     cin >> name[2];
  115.     cout << "\nAge: ";
  116.     cin >> age;
  117.     cout << "\nGender: ";
  118.     cin >> name[3];
  119.  
  120.     adding.push_back({ name[0], name[1], name[2], age, name[3]});
  121.     savedata(adding);
  122.  
  123.     system("cls");
  124.     cout << "\nAdd is Success, Match";
  125.     home();
  126. }
  127. void savedata(list <info> adding) {
  128.     fstream file;
  129.  
  130.     file.open("lname.txt", ios::app);
  131.     for (auto word : adding) {
  132.         file << "\n" << word.lname;
  133.     }
  134.     file.close();
  135.     file.open("fname.txt", ios::app);
  136.     for (auto word : adding) {
  137.         file << "\n" << word.fname;
  138.     }
  139.     file.close();
  140.     file.open("mname.txt", ios::app);
  141.     for (auto word : adding) {
  142.         file << "\n" << word.mname;
  143.     }
  144.     file.close();
  145.     file.open("age.txt", ios::app);
  146.     for (auto word : adding) {
  147.         file << "\n" << word.age;
  148.     }
  149.     file.close();
  150.     file.open("gender.txt", ios::app);
  151.     for (auto word : adding) {
  152.         file << "\n" << word.gender;
  153.     }
  154.     file.close();
  155. }
  156.  
  157. void view() {
  158.     string line[5];
  159.     fstream file[5];
  160.     char choice;
  161.  
  162.     cout << "\nLast Name" << setw(13) << "First Name" << setw(9) << "M.I." << setw(7) << "Age" << setw(11) << "Gender\n";
  163.  
  164.     file[0].open("lname.txt");
  165.     file[1].open("fname.txt");
  166.     file[2].open("mname.txt");
  167.     file[3].open("age.txt");
  168.     file[4].open("gender.txt");
  169.     while (getline(file[0], line[0]) && getline(file[1], line[1]) && getline(file[2], line[2]) && getline(file[3], line[3]) && getline(file[4], line[4])) {
  170.         cout << setw(9) << line[0] << setw(13) << line[1] << setw(9) << line[2] << setw(7) << line[3] << setw(10) << line[4] << "\n";
  171.     }
  172.     for (int h = 0; h < 5; h++) {
  173.         file[h].close();
  174.     }
  175.  
  176.     cout << "\n-------------------------------------------------------------------------"
  177.          << "\nSTUDENT PROFILE INFORMATION SYSTEM -> View"
  178.          << "\n[a] Search"
  179.          << "\n[b] Sort"
  180.          << "\n[c] Home"
  181.          << "\n[d] End"
  182.          << "\nEnter your Choice: ";
  183.     cin >> choice;
  184.     system("cls");
  185.  
  186.     switch (choice) {
  187.     case 'a': {
  188.         cout << "\nLoad is Success, Match";
  189.         search();
  190.         break;
  191.     }
  192.     case 'b': {
  193.         cout << "\nLoad is Success, Match";
  194.         sort();
  195.         break;
  196.     }
  197.     case 'c': {
  198.         cout << "\nLoad is Success, Match";
  199.         home();
  200.         break;
  201.     }
  202.     case 'd': {
  203.         break;
  204.     }
  205.     default: {
  206.         cout << "\nChoice is None, Mismatch";
  207.         view();
  208.     }
  209.     }
  210. }
  211.  
  212. void search() {
  213.     char choice;
  214.  
  215.     cout << "\n------------------------------------------------------------------------"
  216.          << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Search"
  217.          << "\n[a] Last Name"
  218.          << "\n[b] First Name"
  219.          << "\n[c] Middle Initial"
  220.          << "\n[d] Age"
  221.          << "\n[e] Gender"
  222.          << "\n[f] Home"
  223.          << "\n[g] End"
  224.          << "\nEnter your Choice: ";
  225.     cin >> choice;
  226.     system("cls");
  227.  
  228.     switch (choice) {
  229.     case 'a': {
  230.         cout << "\nLoad is Success, Match";
  231.         stringsearch(0, "Last Name");
  232.         break;
  233.     }
  234.     case 'b': {
  235.         cout << "\nLoad is Success, Match";
  236.         stringsearch(1, "First Name");
  237.         break;
  238.     }
  239.     case 'c': {
  240.         cout << "\nLoad is Success, Match";
  241.         stringsearch(2, "Middle Initial");
  242.         break;
  243.     }
  244.     case 'd': {
  245.         cout << "\nLoad is Success, Match";
  246.         stringsearch(3, "Age");
  247.         break;
  248.     }
  249.     case 'e': {
  250.         cout << "\nLoad is Success, Match";
  251.         stringsearch(4, "Gender");
  252.         break;
  253.     }
  254.     case 'f': {
  255.         cout << "\nLoad is Success, Match";
  256.         home();
  257.         break;
  258.     }
  259.     case 'g': {
  260.         break;
  261.     }
  262.     default: {
  263.         cout << "\nChoice is None, Mismatch";
  264.         search();
  265.     }
  266.     }
  267. }
  268.  
  269. void stringsearch(int h, string word) {
  270.     string search;
  271.     fstream file[5];
  272.     string line[5];
  273.     char choice;
  274.  
  275.     cout << "\n-------------------------------------------------------------------------"
  276.          << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Search -> " << word
  277.          << "\nSearch: ";
  278.     cin >> search;
  279.     system("cls");
  280.  
  281.     cout << "\nSearch is Success, Match"
  282.          << "\nLast Name" << setw(13) << "First Name" << setw(9) << "M.I." << setw(7) << "Age" << setw(11) << "Gender\n";
  283.     file[0].open("lname.txt");
  284.     file[1].open("fname.txt");
  285.     file[2].open("mname.txt");
  286.     file[3].open("age.txt");
  287.     file[4].open("gender.txt");
  288.     while (getline(file[0], line[0]) && getline(file[1], line[1]) && getline(file[2], line[2]) && getline(file[3], line[3]) && getline(file[4], line[4])) {
  289.         if (line[h] == search) {
  290.             cout << setw(9) << line[0] << setw(13) << line[1] << setw(9) << line[2] << setw(7) << line[3] << setw(10) << line[4] << "\n";
  291.         }
  292.     }
  293.     for (int i = 0; i < 5; i++) {
  294.         file[i].close();
  295.     }
  296.  
  297.     cout << "\n-------------------------------------------------------------------------"
  298.          << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Search -> " << word
  299.          << "\nSearch: " << search
  300.          << "\n[a] Edit"
  301.          << "\n[b] Delete"
  302.          << "\n[c] Home"
  303.          << "\n[d] End"
  304.          << "\nEnter your Choice: ";
  305.     cin >> choice;
  306.     system("cls");
  307.  
  308.     switch (choice) {
  309.     case 'a': {
  310.         cout << "\nLoad is Success, Match";
  311.         edit(h, word, search);
  312.         break;
  313.     }
  314.     case 'b': {
  315.         cout << "\nLoad is Success, Match";
  316.         deleto(h, word, search);
  317.         break;
  318.     }
  319.     case 'c': {
  320.         cout << "\nLoad is Success, Match";
  321.         home();
  322.         break;
  323.     }
  324.     case 'd': {
  325.         break;
  326.     }
  327.     default: {
  328.         cout << "\nChoice is None, Mismatch";
  329.         stringsearch(h, word);
  330.     }
  331.     }
  332. }
  333.  
  334. void edit(int h, string word, string search) {
  335.     fstream file[5];
  336.     string line[5];
  337.     string edit;
  338.     list <info> editing;
  339.  
  340.     cout << "\n-------------------------------------------------------------------------"
  341.          << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Search -> " << word << "-> Edit"
  342.          << "\nEdit: ";
  343.     cin >> edit;
  344.     system("cls");
  345.  
  346.     file[0].open("lname.txt");
  347.     file[1].open("fname.txt");
  348.     file[2].open("mname.txt");
  349.     file[3].open("age.txt");
  350.     file[4].open("gender.txt");
  351.     while (getline(file[0], line[0]) && getline(file[1], line[1]) && getline(file[2], line[2]) && getline(file[3], line[3]) && getline(file[4], line[4])) {
  352.         int age = stoi(line[3]);
  353.  
  354.         if (line[h] == search) {
  355.             line[h] = edit;
  356.  
  357.             if (word == "Last Name") {
  358.                 editing.push_back({ line[h], line[1], line[2], age, line[4] });
  359.             }
  360.             else if (word == "First Name") {
  361.                 editing.push_back({ line[0], line[h], line[2], age, line[4] });
  362.             }
  363.             else if (word == "Middle Name") {
  364.                 editing.push_back({ line[0], line[1], line[h], age, line[4] });
  365.             }
  366.             else if (word == "Age") {
  367.                 int age = stoi(line[h]);
  368.                 editing.push_back({ line[0], line[1], line[2], age, line[4] });
  369.             }
  370.             else if (word == "Gender") {
  371.                 editing.push_back({ line[0], line[1], line[2], age, line[h] });
  372.             }
  373.         }
  374.         else {
  375.             editing.push_back({ line[0], line[1], line[2], age, line[4] });
  376.         }
  377.     }
  378.     for (int i = 0; i < 5; i++) {
  379.         file[i].close();
  380.     }
  381.  
  382.     savedatav2(editing);
  383.  
  384.     system("cls");
  385.     cout << "\nEdit is Success, Match";
  386.     home();
  387. }
  388.  
  389. void deleto(int h, string word, string search) {
  390.     fstream file[5];
  391.     string line[5];
  392.     string edit;
  393.     list <info> editing;
  394.  
  395.     cout << "\n-------------------------------------------------------------------------"
  396.         << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Search -> " << word << "-> Delete"
  397.         << "\nDelete: ";
  398.     cin >> edit;
  399.     system("cls");
  400.  
  401.     file[0].open("lname.txt");
  402.     file[1].open("fname.txt");
  403.     file[2].open("mname.txt");
  404.     file[3].open("age.txt");
  405.     file[4].open("gender.txt");
  406.     while (getline(file[0], line[0]) && getline(file[1], line[1]) && getline(file[2], line[2]) && getline(file[3], line[3]) && getline(file[4], line[4])) {
  407.         int age = stoi(line[3]);
  408.  
  409.         if (line[h] != search) {
  410.             editing.push_back({ line[0], line[1], line[2], age, line[4] });
  411.         }
  412.     }
  413.     for (int i = 0; i < 5; i++) {
  414.         file[i].close();
  415.     }
  416.  
  417.     savedatav2(editing);
  418.  
  419.     system("cls");
  420.     cout << "\nDelete is Success, Match";
  421.     home();
  422. }
  423.  
  424. void savedatav2(list <info> adding) {
  425.     fstream file;
  426.  
  427.     file.open("lname.txt");
  428.     for (auto word : adding) {
  429.         file << "\n" << word.lname;
  430.     }
  431.     file.close();
  432.     file.open("fname.txt");
  433.     for (auto word : adding) {
  434.         file << "\n" << word.fname;
  435.     }
  436.     file.close();
  437.     file.open("mname.txt");
  438.     for (auto word : adding) {
  439.         file << "\n" << word.mname;
  440.     }
  441.     file.close();
  442.     file.open("age.txt");
  443.     for (auto word : adding) {
  444.         file << "\n" << word.age;
  445.     }
  446.     file.close();
  447.     file.open("gender.txt");
  448.     for (auto word : adding) {
  449.         file << "\n" << word.gender;
  450.     }
  451.     file.close();
  452. }
  453.  
  454. void sort() {
  455.     char choice;
  456.  
  457.     cout << "\n------------------------------------------------------------------------"
  458.         << "\nSTUDENT PROFILE INFORMATION SYSTEM -> Sort"
  459.         << "\n[a] Last Name"
  460.         << "\n[b] First Name"
  461.         << "\n[c] Middle Initial"
  462.         << "\n[d] Age"
  463.         << "\n[e] Gender"
  464.         << "\n[f] Home"
  465.         << "\n[g] End"
  466.         << "\nEnter your Choice: ";
  467.     cin >> choice;
  468.     system("cls");
  469.  
  470.     switch (choice) {
  471.     case 'a': {
  472.         cout << "\nLoad is Success, Match";
  473.         stringsort(0, "Last Name");
  474.         break;
  475.     }
  476.     case 'b': {
  477.         cout << "\nLoad is Success, Match";
  478.         stringsort(1, "First Name");
  479.         break;
  480.     }
  481.     case 'c': {
  482.         cout << "\nLoad is Success, Match";
  483.         stringsort(2, "Middle Initial");
  484.         break;
  485.     }
  486.     case 'd': {
  487.         cout << "\nLoad is Success, Match";
  488.         stringsort(3, "Age");
  489.         break;
  490.     }
  491.     case 'e': {
  492.         cout << "\nLoad is Success, Match";
  493.         stringsort(4, "Gender");
  494.         break;
  495.     }
  496.     case 'f': {
  497.         cout << "\nLoad is Success, Match";
  498.         home();
  499.         break;
  500.     }
  501.     case 'g': {
  502.         break;
  503.     }
  504.     default: {
  505.         cout << "\nChoice is None, Mismatch";
  506.         sort();
  507.     }
  508.     }
  509. }
  510.  
  511. void stringsort(int h, string word) {
  512.     fstream file[5];
  513.     string line[5];
  514.     vector <info> sorting;
  515.  
  516.     file[0].open("lname.txt");
  517.     file[1].open("fname.txt");
  518.     file[2].open("mname.txt");
  519.     file[3].open("age.txt");
  520.     file[4].open("gender.txt");
  521.     while (getline(file[0], line[0]) && getline(file[1], line[1]) && getline(file[2], line[2]) && getline(file[3], line[3]) && getline(file[4], line[4])) {
  522.         int age = stoi(line[3]);
  523.         sorting.push_back({ line[0], line[1], line[2], age, line[4] });
  524.     }
  525.     for (int i = 0; i < 5; i++) {
  526.         file[i].close();
  527.     }
  528.    
  529.     if (word == "Last Name") {
  530.         stringquicksort(sorting);
  531.     }
  532.     else if (word == "First Name") {
  533.         cout << "\nComing Soon...";
  534.     }
  535.     else if (word == "Middle Name") {
  536.         cout << "\nComing Soon...";
  537.     }
  538.     else if (word == "Age") {
  539.         intquicksort(sorting);
  540.     }
  541.     else if (word == "Gender") {
  542.         cout << "\nComing Soon...";
  543.     }
  544.    
  545.     savedatav3(sorting);
  546.     system("cls");
  547.  
  548.     cout << "\nSort is Success, Match";
  549. }
  550.  
  551. void stringquicksort(vector<info>& nums) {
  552.     if (!nums.size()) return;
  553.  
  554.     int left = 0, right = nums.size() - 1;
  555.     while (left <= right) {
  556.         string pivot = nums[left].lname;
  557.         while (left <= right && nums[right].lname < pivot) {
  558.             right--;
  559.         }
  560.         while (left <= right && nums[left].lname > pivot) {
  561.             left++;
  562.         }
  563.         swap(nums[left], nums[right]);
  564.     }
  565.  
  566.     info temp = nums[0];
  567.     nums[0] = nums[right];
  568.     nums[right] = temp;
  569.     stringquicksort(nums);
  570. }
  571.  
  572. void intquicksort(vector<info>& nums) {
  573.     if (!nums.size()) return;
  574.  
  575.     int left = 0, right = nums.size() - 1;
  576.     while (left <= right) {
  577.         int pivot = nums[left].age;
  578.         while (left <= right && nums[right].age < pivot) {
  579.             right--;
  580.         }
  581.         while (left <= right && nums[left].age > pivot) {
  582.             left++;
  583.         }
  584.         swap(nums[left], nums[right]);
  585.     }
  586.  
  587.     info temp = nums[0];
  588.     nums[0] = nums[right];
  589.     nums[right] = temp;
  590.     stringquicksort(nums);
  591. }
  592.  
  593. void savedatav3(vector <info> adding) {
  594.     fstream file;
  595.  
  596.     file.open("lname.txt");
  597.     for (auto word : adding) {
  598.         file << "\n" << word.lname;
  599.     }
  600.     file.close();
  601.     file.open("fname.txt");
  602.     for (auto word : adding) {
  603.         file << "\n" << word.fname;
  604.     }
  605.     file.close();
  606.     file.open("mname.txt");
  607.     for (auto word : adding) {
  608.         file << "\n" << word.mname;
  609.     }
  610.     file.close();
  611.     file.open("age.txt");
  612.     for (auto word : adding) {
  613.         file << "\n" << word.age;
  614.     }
  615.     file.close();
  616.     file.open("gender.txt");
  617.     for (auto word : adding) {
  618.         file << "\n" << word.gender;
  619.     }
  620.     file.close();
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement