Problem with writting to file

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.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>

using namespace std;

class patient
{
    char name[10];
public:
    void get_patient_info();
};
void account_creation();


int main()
{
void account_creation();
}
void account_creation()
{
 patient p1;
 ofstream file;
 file.open("Pateint_info.txt",ios::in);
 p1.get_patient_info();
 file.write()
 file.close();
}
void patient::get_patient_info()
{
    cout<<"please enter patient name: ";
    cin.ignore();
    cin.getline(name, 10);
}


Fix outlined below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
using namespace std;

class patient
{
	char name[10];
public:
	void get_patient_info();
	const char *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
const char *patient::get_name() const { return this->name; }
Hi Smac 89,

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?

regards
Topic archived. No new replies allowed.