Question

In: Computer Science

Write a C++ Program that does the following: As you can see, there is a file...

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

Solutions

Expert Solution

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;


Related Solutions

Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
Write a c++ program that does the following, read temperatures from a file name temp.txt into...
Write a c++ program that does the following, read temperatures from a file name temp.txt into an array, and after reading all the temperatures, output the following information: the average temperature, the minimum temperature, and the total number of temperatures read. Thank you!
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C program that does the following In this part, you will write more complicated...
Write a C program that does the following In this part, you will write more complicated functions. They will require parameters and return values. The purpose is to give you experience with these components, and to show you how functions can be used to break your code down into smaller parts. You will also get some more experience with iterating through arrays.Open repl project Lab: User-Defined Functions 2. Write a program that does the following: 1.(20 pts.) Allows the user...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
You are to write a C++ program which does the following: Reads in the size of...
You are to write a C++ program which does the following: Reads in the size of a list of characters. Reads in the list of characters. Prints the list of characters in the opposite order read in. Prints the list of characters in the order read in. Sorts the list. Prints the sorted list. You may assume there will be no more than 1000 characters in the list. (You should use a constant to make this limit easily changeable.) You...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT