#include <iostream> #include <fstream> #include <string> #include <iomanip> #inc
ID: 3640651 • Letter: #
Question
#include <iostream>#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
const int MAX = 10;
void ProcessCustomer();
void LoadRecords(double balance[], int& numbers_used);
void AddBalance(double balance[], int& number_used);
void Print(double balance[], int number_used);
int main()
{
ProcessCustomer();
return 0;
}
void ProcessCustomer()
{
double balance[MAX];
int number_used;
LoadRecords(balance, number_used);
AddBalance(balance, number_used);
Print(balance, number_used);
system("pause");
}
void LoadRecords(double balance[], int& numbers_used)
{
ifstream infile;
double value;
int index = 0;
infile.open("customers.txt");
infile >> value;
while (! infile.eof())
{
balance[index] = value;
index++;
infile >> value;
}
numbers_used = index;
}
void AddBalance(double balance[], int& number_used)
{
double value;
int index = 0;
ofstream outfile;
outfile.open("customers.txt", ios::app);
do
{
cout << "What is your balance? ";
cin >> value;
balance[index] = value;
outfile << balance[index];
index++;
} while (value > 0);
number_used = index;
}
void Print(double balance[], int number_used)
{
for (int index = 0 ; index < number_used ; index++)
{
cout << index << " " << balance[index] <<" ";
}
}
Explanation / Answer
PS: Please rate the answer I'm not sure what you are trying to do.. But I see some file handling mistakes which I will point out here. Consider LoadRecords function infile.open("customers.txt"); infile >> value; while (! infile.eof()) { balance[index] = value; index++; infile >> value; } Here you are opening customers.txt in read mode and reading values from it till you reach end of file. But when you are exiting, you are not closing the file that you opened. In the AddBalance function, you are opening the same file in append mode. And write all the values user enters until he enters a negative value. outfile.open("customers.txt", ios::app); do { cout > value; balance[index] = value; outfileRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.