Advertisement
KeeganT

Ass75

Oct 24th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. string text, arrayText, arrayText2, exit, line;
  8. string array[1000][5];
  9. int rows=0, rows2=0;
  10. bool noticed=false;
  11.  
  12. void display()
  13. {
  14.     ifstream lineCount("empinfo.txt");
  15.     while(getline(lineCount,text))rows++;
  16.     ifstream file("empinfo.txt");
  17.     for(int c=0;c<rows;c++)file>>array[c][0]>>array[c][1]>>array[c][2]>>array[c][3]>>array[c][4];
  18.     for(int c=0;c<rows;c++)cout<<left<<setw(20)<<array[c][0]<<' '<<setw(20)<<array[c][1]<<' '<<setw(20)<<array[c][2]<<' '<<setw(20)<<array[c][3]<<' '<<array[c][4]<<endl;
  19.     rows2=rows;
  20.     rows=0;
  21.     lineCount.close();
  22.     file.close();
  23. }
  24.  
  25. void menu()
  26. {
  27.         cout<<"1. Display"<<endl;
  28.         cout<<"2. Add"<<endl;
  29.         cout<<"3. Delete"<<endl;
  30.         cout<<"4. Modify"<<endl;
  31.         cout<<"5. Save"<<endl;
  32.         cout<<"6. Exit"<<endl;
  33. }
  34.  
  35. void notice()
  36. {
  37.     cout<<"Important! When entering ANY data, please do not add spaces, separate words with commas."<<endl;
  38.     cout<<"(Example: John,Smith)"<<endl;
  39.     noticed=true;
  40. }
  41.  
  42. int choice(int choose)
  43. {
  44.     if(choose==1)display();
  45.     if(choose==2)
  46.     {
  47.         if(noticed==false)notice();
  48.         cout<<"Add data:"<<endl;
  49.         while(exit!="exit")
  50.         {
  51.             rows++;
  52.             ofstream addInfo("empinfo.txt",ios::app);
  53.             cout<<"Employee Number: ";
  54.             cin>>array[rows][0];
  55.             addInfo<<array[rows][0]<<' ';
  56.             cout<<"Name: ";
  57.             cin>>array[rows][1];
  58.             addInfo<<array[rows][1]<<' ';
  59.             cout<<"Address: ";
  60.             cin>>array[rows][2];
  61.             addInfo<<array[rows][2]<<' ';
  62.             cout<<"Phone Number: ";
  63.             cin>>array[rows][3];
  64.             addInfo<<array[rows][3]<<' ';
  65.             cout<<"City: ";
  66.             cin>>array[rows][4];
  67.             addInfo<<array[rows][4]<<endl;
  68.             addInfo.close();
  69.             cout<<"Type 'exit' to exit to menu."<<endl;
  70.             cin>>exit;
  71.         }
  72.         exit="";
  73.     }
  74.     if(choose==3)
  75.     {
  76.         display();
  77.         string strSearch;
  78.         int num, count=-1;
  79.         cout<<"Enter the name of the Employee who's data you would like to delete:"<<endl;
  80.         cin>>strSearch;
  81.         bool error=false;
  82.         for(int c=0;c<rows2;c++)
  83.         {
  84.             num=array[c][1].find(strSearch);
  85.             count++;
  86.             if(num!=-1)break;
  87.             if(c==rows2-1&&num==-1)
  88.             {
  89.                 cout<<"Error! Name not found! Exiting to menu..."<<endl;
  90.                 error=true;
  91.             }
  92.         }
  93.         if(error==false)
  94.         {
  95.             cout<<"Are you sure you would like to delete the following data?: "<<endl;
  96.             for(int c=0;c<5;c++)
  97.             {
  98.                 cout<<left<<setw(20)<<array[count][c]<<' ';
  99.                 arrayText+=array[count][c];
  100.                 if(c!=4)arrayText+=' ';
  101.             }
  102.             string del;
  103.             cout<<"\nType 'delete' to delete the data, or type 'cancel' to exit to the menu."<<endl;
  104.             cin>>del;
  105.             if(del=="delete")
  106.             {
  107.                 ifstream myfile("empinfo.txt");
  108.                 ofstream temp("empinfo2.txt",ios::app);
  109.                 while(getline(myfile,line))if(line!=arrayText)temp<<line<<endl;
  110.                 myfile.close();
  111.                 temp.close();
  112.                 remove("empinfo.txt");
  113.                 rename("empinfo2.txt","empinfo.txt");
  114.                 cout<<strSearch<<"'s data has been deleted!"<<endl;
  115.             }
  116.             count=-1;
  117.             arrayText="";
  118.             line="";
  119.         }
  120.     }
  121.     if(choose==4)
  122.     {
  123.         if(noticed==false)notice();
  124.         display();
  125.         string strSearch2;
  126.         int num2, count2=-1;
  127.         bool error2=false;
  128.         cout<<"Enter the name of the Employee who's data you would like to delete:"<<endl;
  129.         cin>>strSearch2;
  130.         for(int c=0;c<rows2;c++)
  131.         {
  132.             num2=array[c][1].find(strSearch2);
  133.             count2++;
  134.             if(num2!=-1)break;
  135.             if(c==rows2-1&&num2==-1)
  136.             {
  137.                 cout<<"Error! Name not found! Exiting to menu..."<<endl;
  138.                 error2=true;
  139.             }
  140.         }
  141.         if(error2==false)
  142.         {
  143.             cout<<strSearch2<<"'s data:"<<endl;
  144.             for(int c=0;c<5;c++)
  145.             {
  146.                 cout<<left<<setw(20)<<array[count2][c]<<' ';
  147.                 arrayText2+=array[count2][c];
  148.                 if(c!=4)arrayText2+=' ';
  149.             }
  150.             cout<<endl;
  151.             cout<<"Editing data:"<<endl;
  152.             ifstream myfile2("empinfo.txt");
  153.             ofstream temp2("empinfo2.txt");
  154.             while(getline(myfile2,line))if(line==arrayText2)
  155.             {
  156.                 cout<<"Employee Number: ";
  157.                 cin>>array[count2][0];
  158.                 temp2<<array[count2][0]<<' ';
  159.                 cout<<"Name: ";
  160.                 cin>>array[count2][1];
  161.                 temp2<<array[count2][1]<<' ';
  162.                 cout<<"Address: ";
  163.                 cin>>array[count2][2];
  164.                 temp2<<array[count2][2]<<' ';
  165.                 cout<<"Phone Number: ";
  166.                 cin>>array[count2][3];
  167.                 temp2<<array[count2][3]<<' ';
  168.                 cout<<"City: ";
  169.                 cin>>array[count2][4];
  170.                 temp2<<array[count2][4]<<endl;
  171.             }
  172.             else temp2<<line<<endl;
  173.             myfile2.close();
  174.             temp2.close();
  175.             remove("empinfo.txt");
  176.             rename("empinfo2.txt","empinfo.txt");
  177.             cout<<"Data successfully edited!"<<endl;
  178.             count2=-1;
  179.             arrayText2="";
  180.             line="";
  181.         }
  182.     }
  183.     if(choose==5)cout<<"File saved!"<<endl;
  184. }
  185.  
  186. int main()
  187. {
  188.     int choose;
  189.     while(choose!=6)
  190.     {
  191.         menu();
  192.         cin>>choose;
  193.         while(choose>6)
  194.         {
  195.             cout<<"Invalid choice! The choices are:"<<endl;
  196.             menu();
  197.             cin>>choose;
  198.         }
  199.         choice(choose);
  200.         cout<<endl;
  201.     }
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement