Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- string text, arrayText, arrayText2, exit, line;
- string array[1000][5];
- int rows=0, rows2=0;
- bool noticed=false;
- void display()
- {
- ifstream lineCount("empinfo.txt");
- while(getline(lineCount,text))rows++;
- ifstream file("empinfo.txt");
- for(int c=0;c<rows;c++)file>>array[c][0]>>array[c][1]>>array[c][2]>>array[c][3]>>array[c][4];
- 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;
- rows2=rows;
- rows=0;
- lineCount.close();
- file.close();
- }
- void menu()
- {
- cout<<"1. Display"<<endl;
- cout<<"2. Add"<<endl;
- cout<<"3. Delete"<<endl;
- cout<<"4. Modify"<<endl;
- cout<<"5. Save"<<endl;
- cout<<"6. Exit"<<endl;
- }
- void notice()
- {
- cout<<"Important! When entering ANY data, please do not add spaces, separate words with commas."<<endl;
- cout<<"(Example: John,Smith)"<<endl;
- noticed=true;
- }
- int choice(int choose)
- {
- if(choose==1)display();
- if(choose==2)
- {
- if(noticed==false)notice();
- cout<<"Add data:"<<endl;
- while(exit!="exit")
- {
- rows++;
- ofstream addInfo("empinfo.txt",ios::app);
- cout<<"Employee Number: ";
- cin>>array[rows][0];
- addInfo<<array[rows][0]<<' ';
- cout<<"Name: ";
- cin>>array[rows][1];
- addInfo<<array[rows][1]<<' ';
- cout<<"Address: ";
- cin>>array[rows][2];
- addInfo<<array[rows][2]<<' ';
- cout<<"Phone Number: ";
- cin>>array[rows][3];
- addInfo<<array[rows][3]<<' ';
- cout<<"City: ";
- cin>>array[rows][4];
- addInfo<<array[rows][4]<<endl;
- addInfo.close();
- cout<<"Type 'exit' to exit to menu."<<endl;
- cin>>exit;
- }
- exit="";
- }
- if(choose==3)
- {
- display();
- string strSearch;
- int num, count=-1;
- cout<<"Enter the name of the Employee who's data you would like to delete:"<<endl;
- cin>>strSearch;
- bool error=false;
- for(int c=0;c<rows2;c++)
- {
- num=array[c][1].find(strSearch);
- count++;
- if(num!=-1)break;
- if(c==rows2-1&&num==-1)
- {
- cout<<"Error! Name not found! Exiting to menu..."<<endl;
- error=true;
- }
- }
- if(error==false)
- {
- cout<<"Are you sure you would like to delete the following data?: "<<endl;
- for(int c=0;c<5;c++)
- {
- cout<<left<<setw(20)<<array[count][c]<<' ';
- arrayText+=array[count][c];
- if(c!=4)arrayText+=' ';
- }
- string del;
- cout<<"\nType 'delete' to delete the data, or type 'cancel' to exit to the menu."<<endl;
- cin>>del;
- if(del=="delete")
- {
- ifstream myfile("empinfo.txt");
- ofstream temp("empinfo2.txt",ios::app);
- while(getline(myfile,line))if(line!=arrayText)temp<<line<<endl;
- myfile.close();
- temp.close();
- remove("empinfo.txt");
- rename("empinfo2.txt","empinfo.txt");
- cout<<strSearch<<"'s data has been deleted!"<<endl;
- }
- count=-1;
- arrayText="";
- line="";
- }
- }
- if(choose==4)
- {
- if(noticed==false)notice();
- display();
- string strSearch2;
- int num2, count2=-1;
- bool error2=false;
- cout<<"Enter the name of the Employee who's data you would like to delete:"<<endl;
- cin>>strSearch2;
- for(int c=0;c<rows2;c++)
- {
- num2=array[c][1].find(strSearch2);
- count2++;
- if(num2!=-1)break;
- if(c==rows2-1&&num2==-1)
- {
- cout<<"Error! Name not found! Exiting to menu..."<<endl;
- error2=true;
- }
- }
- if(error2==false)
- {
- cout<<strSearch2<<"'s data:"<<endl;
- for(int c=0;c<5;c++)
- {
- cout<<left<<setw(20)<<array[count2][c]<<' ';
- arrayText2+=array[count2][c];
- if(c!=4)arrayText2+=' ';
- }
- cout<<endl;
- cout<<"Editing data:"<<endl;
- ifstream myfile2("empinfo.txt");
- ofstream temp2("empinfo2.txt");
- while(getline(myfile2,line))if(line==arrayText2)
- {
- cout<<"Employee Number: ";
- cin>>array[count2][0];
- temp2<<array[count2][0]<<' ';
- cout<<"Name: ";
- cin>>array[count2][1];
- temp2<<array[count2][1]<<' ';
- cout<<"Address: ";
- cin>>array[count2][2];
- temp2<<array[count2][2]<<' ';
- cout<<"Phone Number: ";
- cin>>array[count2][3];
- temp2<<array[count2][3]<<' ';
- cout<<"City: ";
- cin>>array[count2][4];
- temp2<<array[count2][4]<<endl;
- }
- else temp2<<line<<endl;
- myfile2.close();
- temp2.close();
- remove("empinfo.txt");
- rename("empinfo2.txt","empinfo.txt");
- cout<<"Data successfully edited!"<<endl;
- count2=-1;
- arrayText2="";
- line="";
- }
- }
- if(choose==5)cout<<"File saved!"<<endl;
- }
- int main()
- {
- int choose;
- while(choose!=6)
- {
- menu();
- cin>>choose;
- while(choose>6)
- {
- cout<<"Invalid choice! The choices are:"<<endl;
- menu();
- cin>>choose;
- }
- choice(choose);
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement