Hello everyone I am just learning my first programming language and have just corrected one error in my code only to be left with another error. Thankfully this is the last problem I need to solve but I’m having a little trouble doing it myself.
The program is to read in a customer number and expenses associated with each customer number. It will then add a finance charge, output all expenses, total everything, and save each customers new balance to file along with their ID number in reverse order.
beginningbalance.dat looks like
111
200.00
300.00
50.00
222
300.00
200.00
100.00
and newbalances.dat SHOULD look like
222
402.00
111
251.00
except depending on where in put the “count–” in the file writing loop, I end up with
222
402.00
111
251.00
-858993460
-92559631349317830000000000000000000000000000000000000000000000.00
I can’t figure out what I should do to get rid of these extra values. Any suggestions?
Here is my code
#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(count >= 0)
{
count--;
new_balance_file <<customer_number[count]<<endl;
new_balance_file<< fixed<<setprecision(2)<<new_balance[count]<<endl;
}
new_balance_file.close();
system("pause");
return 0;
}
Your condition is wrong
Should be
count > 0instead ofcount >= 0.This way count will iterate through values
n, n-1, ... 1and because you decrement it right at the beginning of the while statement, the values you’ll be operating with within your loop will ben-1, n-2.... 0, exactly what you need.By the way, if your course has already covered the
forloop and you’re allowed to use it, then it’s more suitable, like thisHope this helps and good luck with your studies!