Loop help with functions and input/output text files

So I have an assignment to write a program that reads from a text file, manipulates the data, then inserts the manipulated data into an output file. The program works fine if I only have to run the program once. However, the program needs to loop through the input file and run multiple times until the programs receives input of all O's from the "input.txt" file. Below I have provided what I currently have for the program, which is NOT running correctly. If I remove the do loop within the main function, the program will work correctly, but will only run itself once. Below my program, I will include the sample text file provided by my instructor. I am definitely not asking for anyone to re-do my project, but if you could please steer me in the correct direction, it would be appreciated.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

int ReadInputRecord(int&, double&, double&, double&);
void WriteOutputRecord(int&, double&, double&, double&);

int main()
{
int acct_number = 0;
double beg_bal = 0;
double interest_time_period = 0;
double interest_rate = 0;
double interest_earned = 0;
double end_bal = 0;
bool running = true;

do
{
ReadInputRecord(acct_number, beg_bal, interest_time_period, interest_rate);
interest_earned = (beg_bal * interest_time_period * interest_rate);
end_bal = (beg_bal + interest_earned);
WriteOutputRecord(acct_number, beg_bal, interest_earned, end_bal);

if (acct_number == 0 && beg_bal == 0 && interest_time_period == 0 && interest_rate == 0)
{
running = false;
}
} while (running == true);

return 0;
}

int ReadInputRecord(int& new_acct_number, double& new_beg_bal, double& new_interest_time_period, double& new_interest_rate)
{
ifstream input_file;
input_file.open("Input.txt");

//Checking for errors opening file
if (!input_file.is_open())
{
cout << "Error opening input.txt" << endl;
exit(0);
}

input_file >> fixed >> setprecision(2);
input_file >> new_acct_number >> new_beg_bal >> new_interest_time_period >> new_interest_rate;
new_interest_rate = (new_interest_rate / 100);
new_interest_time_period = (new_interest_time_period / 365);

if (new_acct_number == 0 && new_beg_bal == 0 && new_interest_time_period == 0 && new_interest_rate == 0)
{
input_file.close();
exit(0);
}
}

void WriteOutputRecord(int& new_acct_number, double& new_beg_bal, double& new_interest_earned, double& new_end_bal)
{
ofstream output_file;
output_file.open("Output.txt");
const int LEFT_WIDTH = 20;
const int RIGHT_WIDTH = 13;

if (!output_file.is_open())
{
cout << "Error opening output.txt" << endl;
exit(0);
}

if (new_acct_number == 0 && new_beg_bal == 0 && new_interest_earned == 0 && new_end_bal == 0)
{
output_file << "END OF REPORT" << endl;
output_file.close();
exit(0);
}

output_file << fixed << setprecision(2);
output_file << "Account Number: " << new_acct_number << endl;
output_file << endl;
output_file << setfill(' ');
output_file << left << setw(LEFT_WIDTH) << "Beginning Blance:" << right << setw(RIGHT_WIDTH) << new_beg_bal << endl;
output_file << left << setw(LEFT_WIDTH) << "Interest Earned: " << right << setw(RIGHT_WIDTH) << new_interest_earned << endl;
output_file << left << setw(LEFT_WIDTH) << "Ending Balance: " << right << setw(RIGHT_WIDTH) << new_end_bal << endl;
output_file << endl;
output_file << endl;

return;
}

EXAMPLE TEXT FILE (input.txt):
030091128 5000.12 31 4.500
030091129 12534.99 30 5.25
000000000 0.00 0 0.00
One suggestion is to open both input and output files in main(). Pass these by reference as parameters to the functions as required.

In particular the ReadInputRecord() function. You might have this return a bool value true if everything went well, and return false if there was a read error or the values were zeroes.

I show the logic for main() here. The other functions I will leave for you to complete. Note the changed parameter list and that the read function must always return a value. (There is no need to call exit() as the main() function controls the logic for terminating the program).

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

bool ReadInputRecord(ifstream & input_file, int& new_acct_number, double& new_beg_bal, double& new_interest_time_period, double& new_interest_rate);
void WriteOutputRecord(ofstream & output_file, int& new_acct_number, double& new_beg_bal, double& new_interest_earned, double& new_end_bal);

int main()
{
    ifstream input_file("Input.txt");

    // Check for errors opening file
    if (!input_file)
    {
        cout << "Error opening input.txt" << endl;
        return 1;
    }

    ofstream output_file("Output.txt");

    // Check for errors opening file
    if (!output_file)
    {
        cout << "Error opening output.txt" << endl;
        return 1;
    }

    int    acct_number          = 0;
    double beg_bal              = 0;
    double interest_time_period = 0;
    double interest_rate        = 0;

    while ( ReadInputRecord(input_file, acct_number, beg_bal, interest_time_period, interest_rate) )
    {
        double interest_earned = beg_bal * interest_time_period * interest_rate;
        double end_bal         = beg_bal + interest_earned;
        WriteOutputRecord(output_file, acct_number, beg_bal, interest_earned, end_bal);
    } 

    return 0;
}

Topic archived. No new replies allowed.