Loading a txt file back into a vector

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

  1. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    I have a problem with what to do on the load part. The code works fine as follows:

    Add employee
    Delete employee
    save file
    exit

    I have added another option, which is to load the saved file back into the vector. I have tried to write the code, but it goes straight to "Database could not be loaded", and I am stuck. I have only posted the code relevant to load. Name, salary and ID are all in the base class Employee, and Manager, Engineer & Researcher are all derived from that using polymorphism. I can write the load code, to print out the saved file, but cannot write it back into the vector, which is what I am trying to do. So here are the code snippets, the main is the full main.cpp. I want to be able to load the file based on the ID number, which is 1 = manager, 2 = engineer & 3 = researcher. I will only post the code relevant to a manager, as the rest are the same.

    Code:
    //Employee.h
    class Manager : public Employee
    {
    
    public:
    
    	Manager();
    		Manager(const string& fName,
    				 const string& lName,
    				 int sal,
    				 int IDnum,
    				 int meeting,
    				 int holiday);
    
    				 void createEmployee();
    				 void showEmployee();
    				 void save(ofstream& outfile);
    				 void load(ifstream& infile);
    
    				 
    	private:
    
    		int mIDNum;
    		int mNumMeeting;
    		int mNumHoliday;
    };
    
    Code:
    //Employee.cpp
    Manager::Manager()
    {
    	int IDnum = 0;
    	int mNumMeeting = 0;
    	int mNumHoliday = 0;
    }
    
    Manager::Manager(const string& fName, 
    				 const string& lName,
    				 int sal,
    				 int IDnum,
    				 int meeting,
    				 int holiday)
    
    : Employee(fName, lName, sal)
    
    {
    	mIDNum = IDnum;
    	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;
    }
    
    Code:
    //main.cpp
    //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;
    
    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)
    			{
    				ifstream infile ("EmployeeDataBase.txt");
    
    				int IDnum = 0;
    
    				if(infile)
    				{
    
    				for(unsigned int i = 0; i < Database.size(); ++i)
    				Database[i]->load(infile);
    
    				if(IDnum == 1) 
    				{
    				Employee* manager = new Manager;
    				Database.push_back(manager);
    				}
    
    				else if(IDnum == 2)
    
    				{
    				Employee* engineer = new Engineer;
    				Database.push_back(engineer);
    				}
    
    				else if(IDnum == 3)
    				
    				{
    				Employee* researcher = new Researcher;
    				Database.push_back(researcher);
    				}
    
    			else
    				cout << "Database could not be loaded" << endl;
    				cout << endl;
    
    				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
    Please someone help
     
  3. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    All I would like is some help to load a txt file back into the program. I have 3 Managers, 3 Engineers & 3 Researchers stored on the file, with 3 common attributes and a couple of non common attributes.The save file option works fine, and I can read the file to console, just need to be able to write the information back into the vector
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Try adding some debug code, like printing the value of Database.size() just before the loop, add some messages within the load() function so that it's displaying what it's doing etc. The load might be working perfectly.

    IDnum is initialised to zero and never changed, so maybe that's why IDnum==1, IDnum==2 and IDnum==3 always evaluate to FALSE.

    Do you intend for the line "Database->load(infile);" to be the only line included within the for loop?

    In general I think debugging your code would be a *lot* easier if you indented it correctly.
     
  5. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply, and I do need to work on my indenting.

    In my eyes this is what is happening:

    1.Employee counter is saved when you save the file, the idea being it will tell the for loop how many times to execute.

    2. ID num, tells the system if if is a manager, engineer or researcher.

    3. I have created a new manager, engineer and researcher to store the downloaded data in a vector Employee*.

    3. The load->infile part, will load in the class employee objects, and non common objects only applicable to the class, be it manager, engineer or researcher as I have set this in the load method in employee.cpp

    I am stumped on how to get the data to the vector.

    Any advice would be great
     
    Last edited: Oct 30, 2011
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    >> In my eyes this is what is happening:

    That's the problem. You're ignoring what IS happening, and substituting in your brain what you WANT to happen for what IS happening. So you fall into the classic trap: you think there aren't any bugs, so you decide there aren't any bugs. But there are, because it doesn't work as you expect. This is a contradiction, so you decide to yell for help. In reality what's going on here is that there are some bugs in your code so you have to stop thinking that there aren't and start thinking that there are. Then you have to find them.

    >> Employee counter is saved when you save the file, the idea being it will tell the for loop how many times to execute.

    Yes, but it won't do that automatically. What is the value of Database.size() before the loop? Or did you prefer to ignore my advice in favour of posting the above drivel?

    >> Any advice would be great

    Read my first response.
     

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