hi,
My program created an object array containing 271 objects from the "names.txt", and each object has 3 private class variables, "id lastname and firstname "...... i know i can't use ofstream writer to write it to a new file like.... Name name; writer<<name[270];

Anyone please help me understanding how to write each private variable from the object to a new txt document.

I was wondering if i should write 3 of the "get" functions to to the variable out from the object... but since 2 variables are string class.... i don't know how to do it.

Thank you

class Name
{
public:
set(int, string, string);

private
int id;
string lastName, firstName;
}

int main()
{
Name name[300];
ifstream reader;
reader.open("names.txt");
int id, count=0;
string lname, fname;

while (reader>>id>>lname>>fname)
{
name[count].set(id, lname, fname);
count++;
}

ofstream writer;
writer.open("new.txt");

for(int x=0; x<count; x++)
{
writer<<name[x]; //i know this isn't correct,, just to show what i m trying to do
}

return 0;
}


Name::set(int inId, string inLastName, string inFirstName)
{
id= inID;
lastName = inLastName;
firstName = inFirstName;
}

>writer<<name[x]; //i know this isn't correct,, just to show what i m trying to do
Actually, that is correct, provided you overload the << operator for your class:

class Name {
public:
  set(int, string, string);
  friend ostream& operator<< (
    ostream& out, const Name& obj )
  {
    return out<< obj.id <<": "
      << obj.lastName <<", "<< obj.firstName;
  }
private:
  int id;
  string lastName, firstName;
};
commented: thx alot... i just have to study some more about friend " +1

you will need a get function for each of the variables so that you can do this:

writer<<name[x].getID() << " " << writer<<name[x].getLastName() << " "
   << writer<<name[x].getFirstName() << "\n";

[edit]Or do it like Nurue said above[/edit]

you will need a get function for each of the variables so that you can do this:

writer<<name[x].getID() << " " << writer<<name[x].getLastName() << " "
   << writer<<name[x].getFirstName() << "\n";

[edit]Or do it like Nurue said above[/edit]

using the get function was actually my initial attempt,,, however since lastName and firstName are string""" ..... i can't have a simple get function like id as below

int Name:: getID()
{
return id;
}

string lastName, firstName;

would you show me a get function which return a string type?

HI i trıed to compıle your program wıth the get functıon for all the varıables....i got ıt.

//wrıtıng data output the fıle ın the dısk

#include <fstream> //for fıle ınput and output....
#include <iostream>
#include <string>
using namespace std;

class Name
{
public: 
		void set(int inID, string Inlastname, string Infirstname)
{
	id=inID;
	lastName=Inlastname;
	firstName=Infirstname;
}

		int getid()
		{return id;}
		

		string getfirstName()
		{return firstName;}
	
		string getlastName()
		{return lastName;}
		

private: int id;
		 string lastName,firstName;

};
int main() 
    {  
Name name[300];
ifstream reader;
reader.open("names.txt");
int id,count=0;
string  lname,fname;

while(reader>>id>>fname>>lname)
{
	name[count].set(id,lname,fname);
	count++;
}
ofstream writer;
writer.open("names1.txt");

for(int x=0; x<count; x++)

	writer<<name[x].getid()<<' '<<name[x].getfirstName()<<' '<<name[x].getlastName()<<endl;
    return 0;
}// end of main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.