Save function not working properly

My save function seems to have a logic error. The problem is that instead of saving all the objects in the vector, it only saves the last one. Here is the Function:

1
2
3
4
5
6
7
8
9
void	Manager::Save(std::ofstream& outFile)
{
	outFile << "First Name = "	<< mFirstName	<< endl;
	outFile << "Last Name = "	<< mLastName	<< endl;
	outFile << "Salary = "		<< mSalary	<< endl;
	outFile << "MPW	= "		<< mMeetingsPerWeek << endl;
	outFile << "VPY = "		<< mVacationDaysPerYear << endl;
	outFile << endl;
}


and here is the code it is called in:

1
2
3
4
5
6
for(int i = 0; i < database.size(); i++)
{
	std::ofstream outFile("Employee Database.txt");
	database[i]->Save(outFile);
}
break;
When you open the ofstream repeatedly like that, are you sure it isn't wiping the file? I'd suggest just opening the file once outside of the loop.
Oh thanks that was it!
This a very effective way I save files.


#include <iostream>

#include "MySavedData.cpp" //this loads the file so you can recover the values


int main()
{
    char FirstName[] = "Enter First Name";
    char LastName[] = "Enter Last Name";

    //this puts the data on the *.cpp file
    ofstream SaveFile;
    SaveFile.open ("MySavedData.cpp");
    SaveFile << "char[12] FName = \"" << FirstName << "\";" << endl;
    SaveFile << "char[12] LName = \"" << LastName << "\";" << endl;
    SaveFile.close();

    //Loads Values
    cout << "Your first name is:\t" << FName << endl;
    cout << "Your last name is:\t" << FName << endl;

    return 0;


}




But first, make sure the MySavedData.cpp file exist.

Good Luck
Topic archived. No new replies allowed.