Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2006
    Location
    Texas
    Posts
    521

    Help with reading from a text file

    Im writing an employee database for GI and one of requirements for the excercise to save the data, and load it back. But this has me stumped. All of the classes (manager, engineer, and researcher) are in a vector3 array. So I put in a function prototype in the employee class(the above classes extend the employee class) called void SaveStates. (i did mean savestats but ill fix it later )

    Heres the function from the manager class.

    Code:
    void Manager::SaveStates(ofstream outFile);
    {
    	outFile << first << endl;
    	outFile << last << endl;
    	outFile << salary << endl;
    	outFile << meetings << endl;
    	outFile << vacaDays << endl;
    	outFile << endl;
    }
    I also made another function called LoadStates (yes I know its mispelt )

    Code:
    void Manager::LoadStates(ifstream inFile);
    {
    	string garbage;
    	inFile >> garbage >> firstName;
    	inFile >> garbage >> lastName;
    	inFile >> garbage >> salary;
    	inFile >> garbage >> meetings;
    	inFile >> garbage >> vacaDays;
    }
    What I dont understand is how the information well be loaded into the classes constructor and add it self back into the vector3 array.

    My save function from main.cpp

    Code:
    void SaveDatabase()
    {
    	ofstream outFile;
    	outFile.open("employee.txt");
    	for(int i = 0; i < vEmployee.size(); i++)
    		vEmployee[i].SaveStates(outFile);
    	outFile.close();
    }
    I dont understand how to load the saved information back into a class and be put into the array.

  2. #2
    Join Date
    Nov 2006
    Location
    Texas
    Posts
    521
    I wanted to test an idea I had but I get the folowing errors when I compile. /sigh

    error C2509: 'SaveStates' : member function not declared in 'Manager'
    error C2509: 'LoadStates' : member function not declared in 'Manager'

  3. #3
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    The first thing that I notice, is that you have semi-colons on the end of the line containing the function declarations. These should not be there, and are often overlooked when copying from the class definition into the class implementation file.

    You may like to change
    Code:
    void Manager::SaveStates(ofstream outFile);
    {
    	....
    	....
    }
    to
    Code:
    void Manager::SaveStates(ofstream outFile)
    {
    	....
    	....
    }
    Same treatment for LoadStates..

    I'm using MinGW, and if I try to compile code with extra semicolons in these places, I get errors that look like this:
    Code:
    C:\Documents and Settings\enhzflep\My Documents\Code\employeeTest\main.cpp|24|error: declaration of `void manager::SaveStates(char*)' outside of class is not definition
    C:\Documents and Settings\enhzflep\My Documents\Code\employeeTest\main.cpp|25|error: expected unqualified-id before '{' token
    C:\Documents and Settings\enhzflep\My Documents\Code\employeeTest\main.cpp|29|error: declaration of `void manager::LoadStates(char*)' outside of class is not definition
    C:\Documents and Settings\enhzflep\My Documents\Code\employeeTest\main.cpp|30|error: expected unqualified-id before '{' token
    ||=== Build finished: 4 errors, 0 warnings ===|
    Sorry about the wide page.

  4. #4
    Join Date
    Nov 2006
    Location
    Texas
    Posts
    521
    I caught that little mistake after I posted it. The error still persists.

  5. #5
    Join Date
    Nov 2006
    Location
    Texas
    Posts
    521
    I wanted to test an idea I had but I get the folowing errors when I compile. /sigh

    error C2509: 'SaveStates' : member function not declared in 'Manager'
    error C2509: 'LoadStates' : member function not declared in 'Manager'
    I fixed these by using virtual functions sence Manger, Engineer, and Researcher need the same methods but with different stuff.

    But how do i get of this?

    error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

    It just opens the fstream header and points to this:
    Code:
    private:
    	_Myfb _Filebuffer;	// the file buffer
    	};
    that dosent tell me much.

  6. #6
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Hmm, does the compiler point to the line that causes the error, or does it just open up fstream and provide you with the error you mentioned.

    Without seeing the code or having a crack at compiling it, my initial question was "Are you trying to access the _Filebuffer member of ios yourself?" - if so, then no can do - it is private after all!

    If not, then I've seen this type of error occur for a number of reasons - one that does come to mind is the order in which you include the header files. Yeah - strange I know.

    Oh yeah, are you #including fstream, or fstream.h? The fstream.h file has been superseded by fstream. This is another potential cause of conflicts.

    I'd be happy to take a look at the complete code if you want. It's my 3dbuzz username @yahoo.com.au

    S.

  7. #7
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    What you probably want to do is pass to the reading in function a parameter of a reference/pointer to something that will hold the current record. The biggest issue you have with that style file format is not knowing how many records are in before you start. Do remember however with your reading in routine that you have a blank line between records - if you dont take this into account you will find that your records read back munged.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  8. #8
    Join Date
    Jan 2004
    Posts
    76
    Quote Originally Posted by dodlegion View Post
    What I dont understand is how the information well be loaded into the classes constructor and add it self back into the vector3 array.

    I dont understand how to load the saved information back into a class and be put into the array.
    you can't call the constructor of an existing object. If you have to use a member function you will have to create an empty object first and than fill the data in, like you already did.
    For the load funktion I think the easiest way to do it is to use getline.
    To put it into your array I would try it like this:

    void Manager::LoadStats(Employee *myArray, int n)
    {
    *your code here*
    myArray[n] = this;

    }

    hope that helps

    edit: sorry its called getline not readline
    Last edited by DA_HUI; 11-25-2008 at 11:47 AM.

  9. #9
    Join Date
    Nov 2006
    Location
    Texas
    Posts
    521
    Quote Originally Posted by enhzflep View Post
    Hmm, does the compiler point to the line that causes the error, or does it just open up fstream and provide you with the error you mentioned.
    Wont let me run the program at all. Yes it just opens the fstream header, when you double click the error it points to the above code

    Without seeing the code or having a crack at compiling it, my initial question was "Are you trying to access the _Filebuffer member of ios yourself?" - if so, then no can do - it is private after all!
    I dont think I am.

    If not, then I've seen this type of error occur for a number of reasons - one that does come to mind is the order in which you include the header files. Yeah - strange I know.

    Oh yeah, are you #including fstream, or fstream.h? The fstream.h file has been superseded by fstream. This is another potential cause of conflicts.
    My includes are in this order:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include "Employee.h"
    #include "Manager.h"
    #include "Engineer.h"
    #include "Researcher.h"
    using namespace std;
    
    void AddEmployee();
    void DeleteEmployee();
    void SaveDatabase();
    vector<Employee> vEmployee;
    Code:
    void Manager::LoadStats(Employee *myArray, int n)
    {
    *your code here*
    myArray[n] = this;
    
    }
    Im gonna try that soon as this error gets fixed, I thought of something similar.

    Here was what my main class looks like. I dont mind sharing it with people. I open for tips and tricks.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include "Employee.h"
    #include "Manager.h"
    #include "Engineer.h"
    #include "Researcher.h"
    using namespace std;
    
    void AddEmployee();
    void DeleteEmployee();
    void SaveDatabase();
    vector<Employee> vEmployee;
    
    void main()
    {
    	/*ifstream inFile;
    	inFile.open("employee.txt");
    	if (inFile)
    	{
    		inFile.
    	}*/
    
    	bool quit = false;
    	while(!quit)
    	{
    		cout << "vEmployee.size() = " << vEmployee.size() << endl;
    
    		cout << "1) Add an Employee, 2) Delete an Employee, 3) Save Database, 4)Exit. ";
    
    		int input = 0;
    		cin >> input;
    		switch(input)
    		{
    			case 1:
    				AddEmployee();
    				break;
    			case 2:
    				DeleteEmployee();
    				break;
    			case 3:
    				SaveDatabase();
    				break;
    			case 4:
    				quit = true;
    				break;
    		}// switch (INPUT) END braces
    	}// while END braces
    }// main END braces
    
    void AddEmployee()
    {
    	int select = 0;
    	cout << " 1) Manager, 2) Engineer, 3) Researcher.";
    	cin >> select;
    	switch(select)
    	{
    		case 1:
    			{
    				string first;
    				string last;
    				int sal;
    				int meet;
    				int vaca;
    				cout << "Enter First Name" << endl;
    				cin >> first;
    				cout << "Enter Last Name" << endl;
    				cin >> last;
    				cout << "Enter Salary" << endl;
    				cin >> sal;
    				cout << "Enter Number of meetings a week" << endl;
    				cin >> meet;
    				cout << "Enter Number of Vacation days a year" << endl;
    				cin >> vaca;
    				Manager manager(first, last, sal, meet, vaca);
    				vEmployee.push_back(manager);
    
    			} //case END braces
    			break;
    		case 2:
    			{
    				string first;
    				string last;
    				string answer;
    				string type;  // mechanical, electric, software
    				int sal;
    				bool c = false;
    				int experience;
    				cout << "Enter First Name" << endl;
    				cin >> first;
    				cout << "Enter Last Name" << endl;
    				cin >> last;
    				cout << "Enter Salary" << endl;
    				cin >> sal;
    				cout << "Type of Engineer: Mechanical, Electric, Software" << endl;
    				cin >> type;
    				cout << "Knows C++? Yes or No" << endl;
    				cin >> answer;
    				if (answer == "Yes")
    					c = true;
    				else 
    					c = false;
    				cout << "Years of experience" << endl;
    				cin >> experience;
    				Engineer engineer(first,last,type,c,sal,experience);
    				vEmployee.push_back(engineer);
    			}// case2 END braces
    			break; 
    		case 3:
    			{
    				string first;
    				string last;
    				int sal;
    				string schoolName;
    				string thesis;
    				cout << "Enter First Name" << endl;
    				cin >> first;
    				cout << "Enter Last Name" << endl;
    				cin >> last;
    				cout << "Enter Salary" << endl;
    				cin >> sal;
    				cout << "School Graduated from?" << endl;
    				cin >> schoolName;
    				cout << "PhD thesis?" << endl;
    				cin >> thesis;
    				Researcher researcher(first,last,sal, schoolName, thesis);
    				vEmployee.push_back(researcher);
    			}// case3 END braces
    			break;
    		default:
    			cout << "Input not understod" << endl; 
    	} // switch (SELECT)END braces
    }// method END braces
    
    
    
    void DeleteEmployee()
    {
    	int input = 0;
    	cout << "Enter the index of an integer to remove: ";
    	cin >> input;
    	if(input >= 0 && input < vEmployee.size())
    		vEmployee.erase(vEmployee.begin() + input);
    }
    void SaveDatabase()
    {
    	ofstream outFile;
    	outFile.open("employee.txt");
    	for(int i = 0; i < vEmployee.size(); i++)
    		vEmployee[i].SaveStates(outFile);
    	outFile.close();
    }

  10. #10
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Okay. First things first - you need a beter compiler/ide suite, or you missed the message it gave you. The error is in the SaveDatabase() function.

    The line
    vEmployee[i].SaveStates(outFile);
    is the one that's causing you trouble.

    If I change the SaveStates member function of the employee class from

    void SaveStates(ofstream outFile) {}
    to void SaveStates(ofstream &outFile) {}

    The problem goes away. Hope that solves it for you.
    Simon.
    Last edited by enhzflep; 11-26-2008 at 01:48 AM. Reason: misplaced space

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •