I’ve just started learning how to program a few months ago and this is my first programming language. I’ve written up this program to pull a customer number from “BeginningBalance.dat” along with customer purchases and payments.
After reading in the charges for each customer it will add a finance charge and total everything, then it will write the new ending balances for each customer number in the opposite order.
So beginningdata.dat looks like
111
200.00
300.00
50.00
222
300.00
200.00
100.00
and NewBalance.dat should look like
222
402.00
111
251.00
Now all that is being read into NewBalance.dat is
-858993460-9.25596e+061333655222402111251-858993460
Heres my code if anyone can help.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int count = 0;
const int size = 100;
double customer_beg_balance, customer_purchases, customer_payments, finance_charge_cost, finance_charge = .01;
int customer_number[size];
double new_balance[size];
double total_beg_balances=0,total_finance_charges=0,total_purchases=0,total_payments=0,total_end_balances=0;
ifstream beginning_balance;
beginning_balance.open("beginningbalance.dat");
while(beginning_balance>>customer_number[count])
{
beginning_balance >> customer_beg_balance;
beginning_balance >> customer_purchases;
beginning_balance >> customer_payments;
finance_charge_cost = customer_beg_balance * finance_charge;
new_balance[count] = customer_beg_balance + finance_charge_cost + customer_purchases - customer_payments;
total_beg_balances+=customer_beg_balance;
total_finance_charges+=finance_charge_cost;
total_purchases+=customer_purchases;
total_payments+=customer_payments;
total_end_balances+=new_balance[count];
cout<<fixed<<setprecision(2)<<setw(8)
<<"Cust No "<<"Beg. Bal. "<<"Finance Charge "<<"Purchases "<<"Payments "<<"Ending Bal.\n"
<<customer_number[count]<<" "
<<customer_beg_balance<<" "
<<finance_charge_cost<<" "
<<customer_purchases<<" "
<<customer_payments<<" "
<<new_balance[count]<<endl;
count++;
}
cout<<"\nTotals "
<<total_beg_balances<<" "
<<total_finance_charges<<" "
<<total_purchases<<" "
<<total_payments<<" "
<<total_end_balances<<endl;
ofstream new_balance_file;
new_balance_file.open("NewBalance.dat");
while(new_balance_file << customer_number[count] && count >= 0)
{
new_balance_file << new_balance[count];
count--;
}
new_balance_file.close();
system("pause");
return 0;
}
Is not right. You’ll run over the beginning of the array and start negative accesses since you don’t limit count.