Vector Count

Discussion in 'C++' started by tonydav43, Oct 29, 2011.

  1. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    What I need to be able to do is count the number of employees entered in the vector. Then save the file and if I then open the saved txt file in notepad, the display should look like this depending on the amount of employees entered

    EmployeeCount: 2

    ID: 1
    First Name: Tony
    Last Name: Davenport
    mSalary $20000
    Number of meetings to attend per week 2
    Number of holidays per year 25

    ID: 1
    First Name: Angela
    Last Name: Davenport
    mSalary $20000
    Number of meetings to attend per week 3
    Number of holidays per year 25

    I am totally stumped on how to do this, I do have the console displaying, but not saving the amount of employees using the following piece of code in the main.cpp. So just to clarify, once I have got the count sorted, I am then going to use a loop to reload the data back in from the txt file. The loop will run for the amount of employee count, so if the employee count is 2, the for loop will then iterate twice. I have the code for load and the loop, just need something for the employee counter

    Code:
    // Employee.h
    
    # ifndef EMPLOYEE_H
    # define EMPLOYEE_H
    
    #include<iostream>
    #include<string>
    #include<vector>
    #include<fstream>
    
    using namespace std;
    
    class Employee
    {
    	public:
    
    		Employee();
    			Employee(int IDnum,
    					const string& fName,
    					const string& lName,
    					int sal);
    					
    					virtual void createEmployee();
    					virtual void showEmployee();
    					virtual void save(ofstream& outfile);
    					virtual void load(ifstream& infile);
    				 
    					string getFirstName();
    					string getLastName();
    				
    	protected:
    
    		int mIDNum;
    		string mFirstName;
    		string mLastName;
    		int mSalary;
    		
    };
    
    class Manager : public Employee
    {
    
    public:
    
    	Manager();
    		Manager(int IDnum,
    				 const string& fName,
    				 const string& lName,
    				 int sal,
    				 int meeting,
    				 int holiday);
    
    				 void createEmployee();
    				 void showEmployee();
    				 void save(ofstream& outfile);
    				 void load(ifstream& infile);
    
    				 
    	private:
    
    		int mNumMeeting;
    		int mNumHoliday;
    };
    
    class Engineer : public Employee
    {
    public:
    
    	Engineer();
    		Engineer(int IDnum,
    				 const string& fName,
    				 const string& lName,
    				 int sal,
    				 const string& CPlus,
    				 int years,
    				 const string& engType);
    
    				 void createEmployee();
    				 void showEmployee();
    				 void save(ofstream& outfile);
    				 void load(ifstream& infile);
    
    					 
    private:
    
    	string mYCPlus;
    	int mNumYears;
    	string mEngineerType;
    };
    
    class Researcher : public Employee
    {
    public:
    
    	Researcher();
    		Researcher(int IDnum,
    					const string& fName,
    					const string& lName,
    					int sal,
    					const string& school,
    					const string& thesis);
    
    					void createEmployee();
    					void showEmployee();
    					void save(ofstream& outfile);
    					void load(ifstream& infile);
    
    							
    private:
    
    	string mGradSchool;
    	string mPHDThesis;
    };
    
    #endif
    
    Code:
    // Employee.cpp
    
    #include "Employee.h"
    #include<iostream>
    
    using namespace std;
    
    Employee::Employee()
    {
    		int mIDNum = 0;	
    		string mFirstName = "Default";
    		string mlastName = "Default";
    		int sal = 0;
    }
    
    Employee::Employee(int IDnum,
    				   const string& fName,
    				   const string& lName,
    				   int sal)
    {
    		mIDNum = IDnum;	
    		mFirstName = fName;
    		mLastName = lName;
    		mSalary = sal;
    }
    
    void Employee::createEmployee()
    {
    	
    	cout << "First Name: ";
    	cin >> mFirstName;
    	cout << endl;
    
    	cout << "Last Name: ";
    	cin >> mLastName;
    	cout << endl;
    
    	cout << "Salary $";
    	cin >> mSalary;
    	cout << endl;
    
    }
    
    void Employee::showEmployee()
    {
    	cout << "First Name: " << mFirstName;
    	cout << endl;
    	cout << "Last Name: " << mLastName;
    	cout << endl;
    	cout << "Salary $ " << mSalary;
    }
    
    void Employee::save(ofstream& outfile)
    {
    	outfile << "ID number: " << mIDNum << endl;
    	outfile << "First Name: " << mFirstName << endl;
    	outfile << "Last Name: " << mLastName << endl;
    	outfile << "Salary $" << mSalary << endl;
    }
    
    void Employee::load(ifstream& infile)
    {
    	string garbage;
    
    	infile >> garbage >> mFirstName;
    	infile >> garbage >> mLastName;
    	infile >> garbage >> mSalary;
    }
    
    
    string Employee::getFirstName()
    {
    	return mFirstName;
    }
    
    string Employee::getLastName()
    {
    	return mLastName;
    }
    
    Manager::Manager()
    {
    
    	int mNumMeeting = 0;
    	int mNumHoliday = 0;
    }
    
    Manager::Manager(int IDnum,
    				 const string& fName, 
    				 const string& lName,
    				 int sal,
    				 int meeting,
    				 int holiday)
    
    : Employee(mIDNum, fName, lName, sal)
    
    {
    	mNumMeeting = meeting;
    	mNumHoliday = holiday;
    }
    
    void Manager::createEmployee()
    {
    	 Employee::createEmployee();
    
    	mIDNum = 1;
    	cout << "Number of meetings to attend per week ";
    	cin >> mNumMeeting;
    	cout << endl;
    	cout << "Number of holidays per year ";
    	cin >> mNumHoliday;
    	cout << endl;
    }
    
    void Manager::showEmployee()
    {
    	Employee::showEmployee();
    
    	cout << "Number of meetings to attend per week " << mNumMeeting;
    	cout << endl;
    	cout << "Number of holidays per year " << mNumHoliday;
    	cout << endl;
    }
    
    void Manager::save(ofstream& outfile)
    {
    	Employee::save(outfile);
    
    	outfile << "ID number is " << mIDNum << endl;
    	outfile << "Number of meetings to attend per week " << mNumMeeting << endl;
    	outfile << "Number of holidays per year " << mNumHoliday << endl;
    	outfile << endl;
    }
    
    void Manager::load(ifstream& infile)
    {
    
    	Employee::load(infile);
    
    	string garbage;
    	infile >> garbage >> mIDNum;
    	infile >> garbage >> mNumMeeting;
    	infile >> garbage >> mNumHoliday;
    }
    
    Engineer::Engineer()
    {
    	string mYCPlus = "Default";
    	int mNumYears = 0;
    	string mEngineerType = "Default";
    }
    
    Engineer::Engineer(int IDnum,
    				   const string& fName, 
    				   const string& lName,
    				   int sal,
    				   const string& CPlus,
    				   int years,
    				   const string& engType)
    
    : Employee(IDnum, fName, lName, sal)
    
    {
    	mYCPlus = CPlus;
    	mNumYears = years;
    	mEngineerType = engType;
    }
    
    void Engineer::createEmployee()
    {
    	 Employee::createEmployee();
    
    	 mIDNum = 2;
    	 cout << "Knows C++ ";
    	 cin >> mYCPlus;
    	 cout << endl;
    	 cout << "Years of experience: ";
    	 cin >> mNumYears;
    	 cout << endl;
    	 cout << "Engineer type: ";
    	 cin >> mEngineerType;
    	 cout << endl;
    }
    
    void Engineer::showEmployee()
    {
    	Employee::showEmployee();
    
    	cout << "Knows C++ " << mYCPlus;
    	cout << endl;
    	cout << "Years of experience: " << mNumYears;
    	cout << endl;
    	cout << "Engineer type: " << mEngineerType;
    	cout << endl;
    }
    
    void Engineer::save(ofstream& outfile)
    {
    	Employee::save(outfile);
    
    	outfile << "Knows C++ " << mYCPlus << endl;
    	outfile << "Years of experience: " << mNumYears << endl;
    	outfile << "Engineer type: " << mEngineerType << endl;
    	outfile << endl;
    }
    
    void Engineer::load(ifstream& infile)
    {
    
    	Employee::load(infile);
    
    	string garbage;
    
    	infile >> garbage >> mIDNum;
    	infile >> garbage >> mYCPlus;
    	infile >> garbage >> mNumYears;
    	infile >> garbage >> mEngineerType;
    }
    
    Researcher::Researcher()
    {
    	string mGradSchool = "Default";
    	string mPHDThesis = "Default";
    }
    
    Researcher::Researcher(int IDnum,
    					    const string& fName, 
    						const string& lName,
    						int sal,
    						const string& school,
    						const string& thesis)
    
    : Employee(IDnum, fName, lName, sal)
    
    {
    	mGradSchool = school;
    	mPHDThesis = thesis;
    }
    
    void Researcher::createEmployee()
    {
    	 Employee::createEmployee();
    
    	 mIDNum = 3;
    	 cout << "Graduation school was ";
    	 cin >> mGradSchool;
    	 cout << endl;
    	 cout << "Graduated in ";
    	 cin >> mPHDThesis;
    	 cout << endl;
    }
    
    void Researcher::showEmployee()
    {
    	Employee::showEmployee();
    
    	cout << "Graduation school was ";
    	cout << endl;
    	cout << "Graduated in ";
    	cout << endl;
    	
    }
    
    void Researcher::save(ofstream& outfile)
    {
    	Employee::save(outfile);
    
    	outfile << "Graduation school was " << mGradSchool << endl;
    	outfile << "Graduated in " << mPHDThesis << endl;
    	outfile << endl;
    }
    
    void Researcher::load(ifstream& infile)
    {
    
    	Employee::load(infile);
    
    	string garbage;
    	infile >> garbage >> mIDNum;
    	infile >> garbage >> mGradSchool;
    	infile >> garbage >> mPHDThesis;
    }
    
    Code:
    //Main.cpp
    #include <crtdbg.h> // To check for memory leaks
    #define _CRTDBG_MAP_ALLOC // To check for memory leaks
    #include <iostream>
    #include <string>
    #include <vector>
    #include "Employee.h"
    using namespace std;
    
    vector<string> readIn();
    
    int main()
    {
     // To check for memory leaks
    int nDbgFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
    nDbgFlags |= _CRTDBG_LEAK_CHECK_DF;
    nDbgFlags |= _CRTDBG_ALLOC_MEM_DF;
    _CrtSetDbgFlag(nDbgFlags);
    
    	vector<Employee*> Database;
    	int choice = 0;
    	
    	bool quit = false;
    
    	while(!quit)
    	{
    		cout << "Database size = " << Database.size() << endl << endl;
    
    		cout << "Database contains: " << endl << endl;
    
    		for(unsigned int i = 0; i < Database.size(); ++i)
    
    		cout << Database[i]->getFirstName() << " " <<
    		Database[i]->getLastName() << " " << endl;
    			cout << endl << endl;
    			
    
    		cout << "Employee database: Please select an option" << endl;
    		cout << "==========================================" << endl;
    		cout << "1) Add an employee" << endl;
    		cout << "2) Delete an employee" << endl;
    		cout << "3) Save the database" << endl;
    		cout << "4) Load the database" << endl;
    		cout << "5) Exit" << endl << endl;
    
    		cout << "Please enter your selection: ";
    		cin >> choice;
    		cout << endl << endl;
    
    		if(choice <=0 || choice >5)
    		
    		{
    			cout << "Invalid selection,please try again" << endl;
    			continue;
    		}
    		
    		if(choice == 1)
    		{
    			int choice = 0;
    
    			cout << "1) Add Manager" << endl;
    			cout << "2) Add Engineer" << endl;
    			cout << "3) Add Researcher" << endl << endl;
    
    			cout << "Enter selection: ";
    			cin >> choice;
    			cout << endl << endl;
    		
    			switch(choice)
    			{
    			case 1:
    				{
    				Employee* manager = new Manager;
    				manager->createEmployee();
    				Database.push_back(manager);
    
    				break;
    				}
    			case 2: 
    				{
    				Employee* engineer = new Engineer;
    				engineer->createEmployee();
    				Database.push_back(engineer);
    
    				break;
    				}
    			case 3:
    				{
    				Employee* researcher = new Researcher;
    				researcher->createEmployee();
    				Database.push_back(researcher);
    
    				break;
    				}
    			}
    		}
    
    			if(choice == 2)
    			{
    
    				string firstName;
    				string lastName;
    
    				cout << "Please enter the first & last name of the employee to remove: ";
    				cin >> firstName >> lastName;
    				cout << endl;
    				
    			for(unsigned int i = 0; i < Database.size(); ++i)
    			{
    				if(Database[i]->getFirstName()== firstName && Database[i]->getLastName()== lastName )
    				{
    					Database.erase(Database.begin() + i);
    
    					cout << firstName << " " << lastName << " has now been removed, database updated" << endl;
    					cout << endl;
    
    				}
    				else
    				{
    					cout << firstName << " " << lastName << " does not exist" << endl;
    
    					continue;
    
    				}
    			}
    		}
    
    			if(choice == 3)
    
                {
                    ofstream outfile ("EmployeeDataBase.txt");
    
                if(outfile)
    
                {
                  for(unsigned int i = 0; i < Database.size(); ++i)
    
                        Database[i]->save(outfile);
                }
    
                else
    
                    cout << "Could not save the data" << endl;
    
                continue;
    
            }
    
    		if(choice == 4) //This needs the employee count code
    {
    ifstream infile ("EmployeeDataBase.txt");
    
    string garbage;
    int employeeCount = 0;
    if(infile)
    {
        // read EmployeeCount: 4
        infile >> garbage >> employeeCount;
    
        for(int i = 0; i < employeeCount; ++i)
        {
              int id = 0;
              
              // read ID: 1
              infile >> garbage >> id;
    
              if( id == 1 )
              {
                    Manager* manager = new Manager;
                    manager->load(infile);
                    Database.push_back(manager);
              }
              else if( id == 2 )
              {
                  Engineer* engineer = new Engineer;
                  engineer->load(infile);
                  Database.push_back(engineer);
              }
           }
        }
    
    continue;
    } 
    			
    
    
    			
    		if(choice == 5)
    		{
    
    			quit = true;
    
    		break;
    
    		}
    	
    }
    	for(unsigned int i = 0; i < Database.size();++i)
    		delete Database[i];
    
    }
    
     
  2. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Okay, so managed to get the counter done, which has been added to the save option in main, done the code for the load function, runs fine, but does not load the txt file data back into the vector, which is what I am after

    Code:
    
    if(choice == 3)
    
                {
                    ofstream outfile ("EmployeeDataBase.txt");
    
    				outfile << "EmployeeCount; " << Database.size() << endl;
    				cout << endl;
    
                if(outfile)
    
                {
                  for(unsigned int i = 0; i < Database.size(); ++i)
    
                        Database[i]->save(outfile);
                }
    
                else
    
                    cout << "Could not save the data" << endl;
    
                continue;
    
            }
    			if(choice == 4)
    			{
    				ifstream infile("EmployeeDataBase.txt");
    
    				string garbage;
    				int employeeCount = 0;
    
    				if(infile)
    				{
    					for(int i = 0; i < employeeCount; ++i)
    					{
    						int IDnum = 0;
    						infile >> garbage >> IDnum;
    
    						if (IDnum == 1)
    						{
    							Manager* manager = new Manager;
    							manager->load(infile);
    							Database.push_back(manager);
    						}
    
    					else if (IDnum == 2)
    						{
    							Engineer* engineer = new Engineer;
    							engineer->load(infile);
    							Database.push_back(engineer);
    						}
    
    					else if (IDnum == 3)
    						{
    							Researcher* researcher = new Researcher;
    							researcher->load(infile);
    							Database.push_back(researcher);
    						}
    
    						else
    						{
    							cout << "File could not be loaded:" << endl;
    						}
    						continue;
    					}
    				}
    			}
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice