Hi Guys, I have a simple problem with the writing to file. This is a simple code that I want to record 10 patients name in a file. so when the program starts, it calls accounr_creatio(), inside account creation an object of the class is created and then get_patient_info() is called. now I dont know how to return the name and other info which will be added later, from get_patient_info() to account_creation() to write it to the file.
#include <iostream>
#include <fstream>
usingnamespace std;
class patient
{
char name[10];
public:
void get_patient_info();
constchar *get_name() const;
};
void account_creation();
int main()
{
}
void account_creation()
{
patient p1;
// open the file with additional flag ios::ate set
// this prevents overwriting previous patient
// data
ofstream file("Pateint_info.txt", ios::in | ios::ate);
p1.get_patient_info();
// write the patient's name using the getter method
file.write(p1.get_name(), 10);
file.close();
}
void patient::get_patient_info()
{
cout<<"please enter patient name: ";
cin.ignore();
cin.getline(this->name, 10);
}
// getter to return the name of the patient
constchar *patient::get_name() const { returnthis->name; }
Thanks for your replay. I am a bit confused about the pointer which you declared. const char *get_name() const;
and also I read about this pointer but i am not very clear about it, can you please clarify it?