How would I edit the following code so it automatically createsa customer.txt file in order for the code to work? Whenever I runthis code, all I get is the file not found path, and I can’t figureout how to make the program create a customers.txt file.
The code needs to meet these specifications:
Write a C++ program (use dynamic memory allocation) that readsN customer records from a text file(customers.txt) such that the number of therecords is stored on the first line in the file. Each record has 4fields (pieces of information) and stored in the file as shownbelow:
Account Number (integer)
Customer full name (string)
Customer email (string)
Account Balance (double)
The program is expected to print the records in the format shownbelow, sorted in decreasing order based on the account balance:
Account Number : 1201077
Name : Jonathan I. Maletic
Email : jmaletic@ksu.edu
Balance : 10,000.17
————————————-
Note: The records stored in the followingformat (4 lines for each account):
First Line is the account number
Second Line is full name
Third line is an email
Forth line is the available balance
Important Note: You must use 4 dynamic arrays(one for each field) to store all the records (information) readfrom the file. Then sort the arrays and then print it. The programshould open the files and read the first token and use it size forthe dynamic array.
Example (data saved in the text file) |
Output: |
3 1201077 Jonathan I. Maletic jmaletic@ksu.edu 10,000.17 1991999 Saleh M. Alnaeli alnaelis@uwstout.edu 5,000.11 1333333 Bill Bultman Bill.bultman@uwc.edu 120,000.00 |
Account Number : 1333333 Name : Bill Bultman Email :Bill.bultman@uwc.edu Balance :120,000.00 ————————————- Account Number : 1201077 Name : Jonathan I. Maletic Email :jmaletic@ksu.edu Balance :10,000.17 ————————————- Account Number : 1991999 Name : Saleh M. Alnaeli Email :alnaelis@uwstout.edu Balance : 5,000.11 ————————————- |
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
//function prototype
void sortByAccountNumber(int actNum[], string name[], stringemail[], double balance[], int size);
int main()
{
int size;
int i = 0;
string temp;
string fileName = “customers.txt”;
ifstream fin;
fin.open(fileName);
/*checking if file not exists*/
if (!fin)
{
cout << “File not found”<< endl;
system(“pause”);
return EXIT_FAILURE;
}
//read size from file
getline(fin, temp);
size = atoi(temp.c_str());
//create dynamic array of size.
int* actNumber = new int[size];
string* customerName = new string[size];
string* email = new string[size];
double* balance = new double[size];
/*Read data from file */
while (i < size && !fin.eof())
{
getline(fin, temp);
actNumber[i] =atoi(temp.c_str());
getline(fin,customerName[i]);
getline(fin, email[i]);
getline(fin, temp);
int index = temp.find(‘,’);
temp.erase(index, 1);
balance[i] = stold(temp);
i++;
}
/*Calling function, sortByAccountNumber*/
sortByAccountNumber(actNumber, customerName, email,balance, size);
cout << “After sorting by account number(Ascending order) :” << endl;
for (int index = 0; index < size; index++)
{
cout << setw(20) <<“Account Number :”
<<setw(10) << actNumber[index] << endl;
cout << setw(20) <<“Name :”
<<setw(10) << customerName[index] << endl;
cout << setw(20) <<“Email :”
<<setw(10) << email[index] << endl;
cout << setw(20) <<“Balance :”
<<setw(10) << fixed << setprecision(2) <<balance[index] << endl;
}
system(“pause”);
return 0;
}
/*
The function, sortByAccountNumber that takes four arrays andsize
as input arguments and then sort the values based on the accountnumber
in ascending order
*/
void sortByAccountNumber(int actNum[], string name[], stringemail[], double balance[], int size)
{
int i, j;
for (i = 0; i < size – 1; i++)
for (j = 0; j < size – i – 1;j++)
if (actNum[j]> actNum[j + 1])
{
int tempAccount = actNum[j];
actNum[j] = actNum[j + 1];
actNum[j + 1] = tempAccount;
string tempName = name[j];
name[j] = name[j + 1];
name[j + 1] = tempName;
string tempemail = email[j];
email[j] = email[j + 1];
email[j + 1] = tempemail;
double tempBalance = balance[j];
balance[j] = balance[j + 1];
balance[j + 1] = tempBalance;
}
}
Expert Answer
Answer to How would I edit the following code so it automatically creates a customer.txt file in order for the code to work? Whene…