#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Name
{
public:
string firstname;
string lastname;
};
class Adress
{
public:
string housenum;
string stname;
};
class Person
{
Name name;
Adress adress;
public:
void readData(ifstream &cfile);
void printData(ofstream &outfile);
};
void read(Person); // function prototypes
void print(Person);
int main()
{
Person jeffrey;
cout << " Please enter a first and last name " << endl;
read(jeffrey);
print(jeffrey);
return 0;
}
I am trying to get the function read
to call the member function readData
to read in the name. But when the program is compiled and run. I get prompted to enter the name , but then after that it does not output it. It looks like it outputs a null string. It works and outputs when readData
and printData
are called directly from main
but not when called from the other functions. Help Please.
void read(Person M)
{
ifstream cfile("con");
M.readData(cfile);
cfile.close();
return;
}
void print(Person M)
{
ofstream outfile("con");
M.printData(outfile);
outfile.close();
return;
}
void Person::readData(ifstream &cfile)
{
cfile >> name.firstname;
cfile >> name.lastname;
return;
}
void Person::printData(ofstream &outfile)
{
outfile << name.firstname << endl;
outfile << name.lastname << endl;
return;
}