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:
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