Error in placing output to txt using <fstream>

Hey this is my second time posting here, I have another question to ask. I wanted to write a text file recording the patient's particular and medical fees. But I encounter 2 problems.

1) I am using 2 different classes, class Patient and class Fee and using inheritence doesnt solve the problem.

2) I try to seperate the file this way although I think it's wrong.

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
42
43
44
45
46
47
48
void Patient::Invoice0(void)
{	ofstream outFile;
	outFile.open("Patient Fee.txt");
	outFile<<"Seng Tock Tan Hospital"<<endl;
	outFile<<"Name : "<<name<<endl<<"NRIC : "<<NRIC<<endl<<"Phone Number : "<<phoneNumber<<endl;
		
	outFile.close();
}
void Fee::Invoice(void)
{
	ofstream outFile;
	outFile.open("Patient Fee.txt");
	//outFile<<"Seng Tock Tan Hospital"<<endl;
	//outFile<<"Name : "<<name<<endl<<"NRIC : "<<NRIC<<endl<<"Phone Number : "<<phoneNumber<<endl;
	outFile<<setfill('*')<<setw(90)<<"\n\t\t\tPatient' Fee schedule"<<endl;
	outFile<<setfill('*')<<setw(66)<<"*\n";
	outFile<<"\t\t\tCLASS OF WARD ACCOMONDATION\n";
	outFile<<setfill('*')<<setw(66)<<"\n";
	outFile<<"\t\t\t"<<"A\t\t"<<"B\t\t"<<"C\t\t\n";
	outFile<<"Acute Ward\t"<<"\t$230"<<"\t\t$150"<<"\t\t$75\n";
	outFile<<"Intensive care unit"<<"\t$500"<<"\t\t$300"<<"\t\t$150\n";
	outFile<<"Consultation Fee"<<"\t$50"<<"\t\t$40"<<"\t\t$25\n";
	outFile<<"Total\t\t"<<"\t$780"<<"\t\t$490"<<"\t\t$250\n";
	outFile<<setfill('*')<<setw(66)<<"\n\n";
	outFile<<"You have choosed Ward "<<choice<<endl;
	outFile<<"This ward will cost you $"<<price<<endl<<endl<<endl;
	outFile<<"You had stayed in this hospital for "<<day<<" days."<<endl;
	outFile<<setfill('*')<<setw(80)<<"\n\t\t\tOther Fees"<<endl;
	outFile<<setfill('*')<<setw(67)<<"*\n";
	outFile<<"Type 1:Back surgery\t$7800\nType 2:Ear surgery\t$4900\nType 3:Plastic surgery\t$5000\nType 4:Nose surgery\t$3500\nType 5:Leg surgery\t$6000\n";
	outFile<<setfill('*')<<setw(67)<<"\n";
	outFile<<"You have choosed surgery type "<<surgery<<endl;
	outFile<<"This will cost you another $"<<others<<endl<<endl<<endl;
	outFile<<setfill('~')<<setw(67)<<"\n";
	outFile<<"Type 1:A\t$1000\nType 2:B\t$2500\nType 3:C\t$1500\nType 4:D\t$1700\nType 5:E\t$600\n";
	outFile<<"You have choosed surgery type "<<surgery<<endl;
	outFile<<"This will cost you another $"<<others<<endl<<endl<<endl;
	outFile<<setfill('~')<<setw(67)<<"\n\n\n";
	outFile<<"The total payment is $"<<Total;

	

		
	
	outFile.close();


}


Because the second function might just overwrite the first.

Note: There's no error while building or compiling, but the output only shows the second function and totally ignoring the first.

Thanks in advance.
To open a file for appending you'll need to use the ios::ate flag. Reference documentation here:
http://www.cplusplus.com/reference/iostream/fstream/open.html

Good luck!
I am looking at it now, thanks Duoas, presentation of my project is today *nervous*
YES IT WORKS! ^^ thank you so much Duoas.

May i ask what's the difference between ofstream and fstream? Or are they the same?

I can't believe the school took out the ios::ate flag from the chapter, it's going to be so useful and efficient since we are dealing with classes. tsk.

THANKS AGAIN!
Schools often lag behind standards... Alas.

An fstream is derived from both an ifstream and an ofstream, so it can do both input and output.

Good luck on your project!
wait! There's a problem.

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
void Patient::Invoice0(void)
{
	fstream outFile;
	outFile.open("Patient Fee.txt");
	outFile<<"Seng Tock Tan Hospital"<<endl;
	outFile<<"Name : "<<name<<endl<<"NRIC : "<<NRIC<<endl<<"Phone Number : "<<phoneNumber<<endl<<endl;
	outFile.close();
}
void Fee::Invoice(void)
{
	fstream outFile;
	outFile.open("Patient Fee.txt", fstream::in|fstream::app);
	outFile<<endl<<endl;
	outFile<<setfill('*')<<setw(80)<<"\n\t\t\tPatient' Fee schedule"<<endl;
	outFile<<setfill('*')<<setw(66)<<"*\n";
	outFile<<"\t\t\tCLASS OF WARD ACCOMONDATION\n";
	outFile<<setfill('*')<<setw(66)<<"\n";
	outFile<<"\t\t\t"<<"A\t\t"<<"B\t\t"<<"C\t\t\n";
	outFile<<"Acute Ward\t"<<"\t$230"<<"\t\t$150"<<"\t\t$75\n";
	outFile<<"Intensive care unit"<<"\t$500"<<"\t\t$300"<<"\t\t$150\n";
	outFile<<"Consultation Fee"<<"\t$50"<<"\t\t$40"<<"\t\t$25\n";
	outFile<<"Total\t\t"<<"\t$780"<<"\t\t$490"<<"\t\t$250\n";
	outFile<<setfill('*')<<setw(66)<<"\n\n";
	outFile<<"You have choosed Ward "<<choice<<endl;
	outFile<<"This ward will cost you $"<<price<<endl<<endl<<endl;
	outFile<<"You had stayed in this hospital for "<<day<<" days."<<endl;
	outFile<<setfill('*')<<setw(80)<<"\n\t\t\tOther Fees"<<endl;
	outFile<<setfill('*')<<setw(67)<<"*\n";
	outFile<<"Type 1:Back surgery\t$7800\nType 2:Ear surgery\t$4900\nType 3:Plastic surgery\t$5000\nType 4:Nose surgery\t$3500\nType 5:Leg surgery\t$6000\n";
	outFile<<setfill('*')<<setw(67)<<"\n";
	outFile<<"You have choosed surgery type "<<surgery<<endl;
	outFile<<"This will cost you another $"<<others<<endl<<endl<<endl;
	outFile<<setfill('~')<<setw(67)<<"\n";
	outFile<<"Type 1:A\t$1000\nType 2:B\t$2500\nType 3:C\t$1500\nType 4:D\t$1700\nType 5:E\t$600\n";
	outFile<<"You have choosed medication type "<<medication<<endl;
	outFile<<"This will cost you another $"<<med<<endl<<endl<<endl;
	outFile<<setfill('~')<<setw(67)<<"\n\n\n";
	outFile<<"The total payment is $"<<Total;

	outFile.close();
}


There's a problem. My textfile will not overwrite the previous data with ios::trunc flag. It continues on and on, i want it to start on a fresh page everytime it resets. Any solutions?
I see no trunc there. Make sure line 4 reads:

outFile.open("Patient Fee.txt",ios::out|ios::trunc);

Also, on line 12 you tell the computer that you are going to read from the file only. It should say:

outFile.open("Patient Fee.txt",ios::out|ios::app);

Hope this helps.
Last edited on
Problem solved.

Thank You very much, my group has scored 49/50 for program layout, output and interface.

Another 50 marks will go to individual, feel much more relief and free now.

Thank You for your help! I will repay you if I know your address :P
Your enjoyment at having learned something is payment enough. :-)
Topic archived. No new replies allowed.