In: Computer Science
Write a C++ Program that does the following:
As you can see, there is a file named "invoice1_test1.txt". You are to use this file as your input file for your program. Do the following:
1. Sort the file by last name using an array. You can use any of the sorting algorithms we have previously used in class. You may use the string data type to store text data.
2. Compute the following:
a. The total balance due using the BalanceDue column.
b. Find the customer(s) with the highest number of rental
days.
c. Find the customer(s) with the highest balance due.
Here's the file
invoice1_test1.txt
LastName FirstName DaysofRental BalanceDue
Smith Joe 15 100.50
Doe John 10 95.20
Anderson Paul 30 20.00
ODonell Miriam 10 24.30
Foster Sam 30 15.00
Zom Pete 10 20.00
Mock Chilly 100 30
Smitty Chris 200 200
Xu Conor 1 200
Anilo Steve 0 0
C++ CODE:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
fin.open("invoice1_test1.txt");
int n=0;
string line;
while(fin)
{
getline(fin,line);
n++;
}
n-=2;
fin.close();
string lastname[n];
string firstname[n];
int daysofrental[n];
float balancedue[n];
fin.open("invoice1_test1.txt");
getline(fin,line);
for(int i=0;i<n;i++)
{
fin>>lastname[i];
fin>>firstname[i];
fin>>daysofrental[i];
fin>>balancedue[i];
}
//Sorting according to last name
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(lastname[i]>lastname[j])
{
swap(lastname[i],lastname[j]);
swap(firstname[i],firstname[j]);
swap(daysofrental[i],daysofrental[j]);
swap(balancedue[i],balancedue[j]);
}
}
}
//Sorted data display
cout<<"Sorted Data: "<<endl;
for(int i=0;i<n;i++)
cout<<lastname[i]<<" "<<firstname[i]<<"
"<<daysofrental[i]<<"
"<<balancedue[i]<<endl;
float total_balance_due=0;
for(int i=0;i<n;i++)
total_balance_due+=balancedue[i];
cout<<"Total balance due:
"<<total_balance_due<<endl;
int maxdays=0;
for(int i=0;i<n;i++)
{
if(maxdays<daysofrental[i])
maxdays=daysofrental[i];
}
int maxdue=0;
for(int i=0;i<n;i++)
{
if(maxdue<balancedue[i])
maxdue=balancedue[i];
}
cout<<"Highest rental days are "<<maxdays<<"
of:"<<endl;
for(int i=0;i<n;i++)
{
if(maxdays==daysofrental[i])
cout<<firstname[i]<<"
"<<lastname[i]<<endl;
}
cout<<"Highest balance due is "<<maxdue<<"
of:"<<endl;
for(int i=0;i<n;i++)
{
if(maxdue==balancedue[i])
cout<<firstname[i]<<"
"<<lastname[i]<<endl;
}
return 0;
}
Input file: invoice1_test1.txt
Output;