0
void EmployeeSearch()
{
vector<string> file;
string temp;

ifstream infile("GROUP.txt");

while( !infile.eof() )
{
	getline(infile, temp);
	file.push_back(temp);
}
// done reading file
infile.close();

string item;

cout << "Enter an employee number to delete from the file: ";
getline(cin, item);

for(int i = 0; i < (int)file.size(); ++i)
{
	if(file[i].substr(0, item.length()) == item)
	{
					
		file.erase(file.begin() + i);
		cout << "Order erased!"<< endl;
		i = 0; // Reset search
	}
}

This code works fine and deletes the appropriate record when ran in its own program. However, when i implement this as a method within my class, it does not work and deletes every record in the file. Any help would be greatly appreciated.

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by jimmyo88
0
/////////////////////////////
//////CLASS EMPLOYEE///////////////
class Employee
{
      private:
      string EmployeeID, EmployeeFirstName, EmployeeLastName,;
      double Wage;
      
      public:
      
//////////////////////////////////////////
//////CLASS FUNCTION AddEmployee//////////      
void AddEmployee()   
    {  
           
    char choice;
    cout  <<"Enter Employee ID: ";
    cin >> EmployeeID;

      cout << "\n";
     cout<<"Enter Employee First Name: ";
     cin >> EmployeeFirstName;
     cout << "\n";
     cout<<"Enter Employee Last Name: ";
     cin >> EmployeeLastName;
     cout << "\n";
     cout<< "Enter Employee Wage: ";
     cin >> Wage;  
     cout << "\n\n";  
     cout << "ID Number:  " << EmployeeID << "  F.Name:  " << EmployeeFirstName << "  L.Name:  " << EmployeeLastName << "  Wage: " << Wage << "\n\n";

     cout << "Would you like to write this record to file? y/n?";
     cin >> choice;
     
     
     fstream file;
     file.open("GROUP.txt", ios::app | ios::out);

     
   if (choice != 'n')
   {
   
      if (file.is_open())
      {
      file << EmployeeID << "," << EmployeeFirstName << "," << EmployeeLastName << "," << Wage << ",\n\n";
      cout << "Wrote to the file";
      }
   else
   {
       cout << "Record not saved";
   }
   
}  
     else
     {
     cout << "Error reading the file";
     }
 
     file.close();
         
         }


 
//////////////////////////////////////////
//////CLASS FUNCTION Employeedelete//////////
void EmployeeSearch()
{
vector<string> file;
string temp;

ifstream infile("GROUP.txt");

while( !infile.eof() )
{
	getline(infile, temp);
	file.push_back(temp);
}
// done reading file
infile.close();

string item;

cout << "Enter an employee number to delete from the file: ";
getline(cin, item);

for(int i = 0; i < (int)file.size(); ++i)
{
	if(file[i].substr(0, item.length()) == item)
	{
					
		file.erase(file.begin() + i);
		cout << "Order erased!"<< endl;
		i = 0; // Reset search
	}
}

//write new order list back out
ofstream out("GROUP.txt", ios::out | ios::trunc);

for(vector<string>::const_iterator i = file.begin(); i != file.end(); ++i)
{
	out << *i << endl;
}
out.close();
}

//////////////////////////////////////////
//////CLASS FUNCTION EmployeeEDIT//////////
void EmployeeEdit()
{
    ifstream data("GROUP.txt");
    string item,line;
    int x=0;
    int y=0;
    string id;

    cout<<endl;
    cout<<"Enter Employee Id: ";
    cin>>id;
    cout<<""<<endl;

    while(!data.eof())
    {
        double wage; 
        
        char Choice; //declare char choice
        getline(data,line); //get each line and store it in data
        string item_array[10], NewWage; //declare strings item_array and NewWage
        stringstream stream(line);
        x=0,y=0;

        while(getline(stream,item,','))
        {
            item_array[x]=item;
            x++;
            item_array[y]=item;
            y++;
            

        }

        if(item_array[0]==id)
        {
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tID:   " <<right<<"  "<<item_array[0]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tFirst Name: " <<right<<" "<<item_array[1]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tLast Name: " <<right<<" "<<item_array[2]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tWage: " <<right<<" "<<item_array[3]<<endl;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tWage: " <<right<<" "<<item_array[4]<<endl;

            
            
            cout << "Would you like to change the Wage? Y/N ";
            cin >> Choice;    
            
            if (Choice == 'y')
            {
            cout << "What would you like to change the wage too?";
            cin >> NewWage;
            
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tCurrent Wage: " <<right<<" "<<item_array[3]<<endl;
            
            item_array[3] = NewWage;
            cout<<setfill('-')<<left<<setw(10)<<"\t\t\tCurrent Wage: " <<right<<" "<<item_array[3]<<endl;
            }
        }
    }
    data.close();
}


//////////////////////////////////////////
//////CLASS FUNCTION DisplayAllEmployees//////////
               
void DisplayAllEmployees()
{
    string line;
    ifstream x ("GROUP.txt");

    if (x.is_open())
    {
    while(!x.eof())
    {

    cout<<endl;
    getline(x,line);
    cout<<line<<endl;

    }
    x.close();
    }
    else
    cout<<"Cant open file."<<endl;
}


};

This is my code for the class. Please ignore the employee edit bit. I've not got this working yet.
Many thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.