Not outputting to file

Hi the code is suppose to output to file but the whole program crashes before it outputs anything. What is wrong with it?

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  #include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

//Function Prototypes
void input(double& balance, double& interest_rate, double& min_percent);
void calculation(double& balance, double interest_rate, double min_percent, double& interest, double& min, double& sum_interest);
void output(double balance, double min, double interest, double sum_interest, int month, ofstream& fileout);
void output_header(double balance, double interest_rate, double min_percent);
int main()
{
    //Declare variables
    double balance, interest_rate, min_percent;
    int month = 1;
    double interest, min, sum_interest;
    
    ofstream fileout;
    fileout.open("lab6out.txt");
    fileout << setprecision(2) << fixed;
    
    //Input balance, interest rate, and minimum payment percent
    input(balance, interest_rate, min_percent);
    
    //Output information header
    output_header(balance, interest_rate, min_percent);
    
    cout << setprecision(2) << fixed;
    
    while(balance > 0)
    {
    
     //Calculate balance, interest, and minimum payment
     calculation(balance, interest_rate, min_percent, interest, min, sum_interest);
     
     //Output information
     output(balance, min, interest, sum_interest, month, fileout);
     month++;
     }
   
     
     
    system("pause");
    return 0;
} 
void input(double& balance, double& interest_rate, double& min_percent)
{    
     /*Pre: balance - reference to balance
     interest_rate - reference to interest rate
     min_percent - reference to the minimum payment percent
     Post - nothing
     Purpose: input data from user
     */
     
    ofstream outputFile;
	outputFile.open("lab6.txt");
	
    cout << "Enter the account balance: ";
	cin >> balance;

	cout << "Enter the interest rate: ";
	cin >> interest_rate;

	cout << "Enter the minimum payment percentage: ";
	cin >> min_percent;
	
    outputFile << balance << endl;
	outputFile << interest_rate << endl;
	outputFile << min_percent << endl;
	
	ifstream inputfile;
	inputfile.open("lab6.txt");
	
    inputfile >> balance;
    inputfile >> interest_rate;
    inputfile >> min_percent;
}

void calculation(double& balance, double interest_rate, double min_percent, double& interest, double& min, double& sum_interest)
{
     /*Pre: balance - reference to balance
     interes_rate - interest rate
     min_percent - minimum payment percent
     interest - reference to interest incurred
     min - refernce to minimum payment paid
     sum_interest - reference to total interest paid
     Post: nothing 
     Purpose: to calculate payment schedule
     */
     
     interest = (balance * interest_rate/100)/12;
     balance = balance + interest;
     min = (balance * min_percent/100);
     if (balance < 15)
     {
        min = balance;
        balance == 0;
     }
     else if (min < 15)
     {
          min = 15;
     } 
     balance = balance - min;
     sum_interest = sum_interest + interest;
}
void output_header(double balance, double interest_rate, double min_percent)
{
     /* Pre: balance - balance
     interest_rate - interest rate
     min_percent - minimum payment percent
     Post: nothing 
     Purpose: output header
     */
     
     cout << "Balance Owing: " << balance << endl;
     cout << "APR as %: " << interest_rate << endl;
     cout << "Percent for minimum payment as %: " << min_percent << endl;
     cout << "Month"  << setw(10) << "Balance" << setw(10) << "Interest" << setw(10) << " Minimum" << setw(10) << "Sum of" << endl; 
     cout << setw(27) << "this month" << setw(24) << "interest paid" << endl;
}


void output(double balance, double min, double interest, double sum_interest, int month, ofstream& fileout)
{
     /* Pre: balance - balance
     min - minimum payment paid
     interest - interest incurred
     sum_interest - total interest paid
     month - month
     Post: nothing
     Purpose: output payment schedule
     */
     
     
     fileout << right << setw(5) << month << right << setw(10) <<  balance << setw(10) << interest << setw(10) << min << setw(10) << sum_interest << endl;         
          
     
}
Maybe you should only open the same file from one place at the same time.
I thought of that but my professor wants to different files.
In the input function are writing to the file while the same file is already opened for reading. I think you should at least close outputFile before opening inputFile and see if that solves your problems.
Last edited on
Topic archived. No new replies allowed.