Writing output of while loop to text file!

Hello everyone! I recently looked up how to write your output to a text file and came across this tutorial http://www.cprogramming.com/tutorial/lesson10.html which was very helpful. However some problems are still coming up when I try to write my output to a file. I am building a life insurance premium calculator with c++. What my calculator does is read in a text file called "policies.txt" containing a number of different policies. For example:
1
2
3
4
Pol1 M N 20 100000 1 .04 99
Pol2 F S 30 100000 1 .05 99
Pol3 M S 72 750000 1 .03 99
Pol4 F N 45 1000000 1 .05 99


And using this, it creates a single premium. Once the calculator calculates the premium of each policy, I want it to write out the premium amount of each policy to a textfile called "output.txt".
The problem is that when I check my "output.txt" file, only the premium for the last policy shows up. It only reads:
Premium for Pol4 is 220384
When I want it to read:
1
2
3
4
Premium for Pol1 is 14101.6
Premium for Pol2 is 14221.2
Premium for Pol3 is 582391
Premium for Pol4 is 220384


How come only the last policy is showing up? and is there any way to make all four policies appear in my output text file? Thank you!!
Here is my code:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
double ratesmn[86] = {
#include "MaleNonSmoker.txt"

	- 1
};
double ratesms[86] = {
#include "MaleSmoker.txt"

	- 1
};
double ratesfn[86] = {
#include "FemaleNonsmoker.txt"

	- 1
};
double ratesfs[86] = {
#include "FemaleSmoker.txt"

	- 1
};
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
#include <stdio.h>
using namespace std;
#define STARTAGE 15
int main(int argc, char ** argv) {
	ofstream out_data("output.dat");
	double tpx[100];
	double myrate[100];
	double T[100];
	double vT[100];
	double px[100];
	double ax[100];
	double const *rates;
	string line;
	string age, death_ben, interest, end_age;
	string policy, gender, smoker_status, db_option;
	ifstream datafile("Policies.txt");

	if (datafile.is_open()){
		while (!datafile.eof()){
			datafile >> policy >> gender >> smoker_status >> age >> death_ben >> db_option >> interest >> end_age;
			int Age;
			int DeathBen;
			double Interest;
			int endAge;
			if (!(istringstream(age) >> Age)) Age = 0;
			if (!(istringstream(death_ben) >> DeathBen)) DeathBen = 0;
			if (!(istringstream(interest) >> Interest)) Interest = 0;
			if (!(istringstream(end_age) >> endAge)) endAge = 0;
			if (gender == "M"){
				if (smoker_status == "S"){
					rates = ratesms;
				}
				else{
					rates = ratesmn;
				}
			}
			else{
				if (smoker_status == "S") {
					rates = ratesfs;
				}
				else{
					rates = ratesfn;
				}
			}
			double sum = 0;
			tpx[Age] = 1;
			T[Age] = 1;
			vT[Age] = 1 / pow(1+Interest, T[Age]);
			while ((Age - STARTAGE) < endAge+1 - STARTAGE){
				myrate[Age]= rates[Age - STARTAGE];
				px[Age] = 1 - rates[Age - STARTAGE];
				ax[Age] = myrate[Age] * tpx[Age] * vT[Age];
				Age = Age + 1; 
				tpx[Age] = tpx[Age - 1] * px[Age - 1];
				T[Age] = T[Age - 1] + 1;
				vT[Age] = 1 / pow(1 + Interest, T[Age]);
			}
			int otherAge;
			if (!(istringstream(age) >> otherAge)) otherAge = 0;
			for (int i = otherAge; i < 100; i++){
				sum += ax[i];
			}
			ofstream a_file("output.txt");
			a_file << "Premium for " << policy << " is " << sum*DeathBen << "\n";
		}
	}
	printf(" Press enter to exit\n");
	getchar();
	return 0;
}
This is because you keep opening the file to write to it every iteration (line 88). You should only have to open the file once and after writing everything then close it
That worked! Thank you so much
Topic archived. No new replies allowed.