In: Computer Science
I have to create a program that:
I have attached what I have so far. All I need to do is get help with the next two tables which will sort by last name, alphabetically and then sort by total sales in descending order. I need help adding the segments that will output two more tables of sorting by last name and descending total sales. If possible, can you add comments to show what you did and what it means.
#include <iostream> #include <string> #include <fstream> #include <sstream> #include <algorithm> // for std::sort using namespace std; class customer{ public: string first; string last; string state; double sHistory[3]; // Sales history for three years. double totalSales; // Total sales (adding all three years together) int purchaseUnits; }; void printcust(customer[], int); void sortname(customer[], int); void sortsales(customer[], int); int main () { fstream infile; customer cust; customer custarray[10]; infile.open("data.txt"); int i = 0; while(infile) { infile >> custarray[i].first; infile >> custarray[i].last; infile >> custarray[i].state; infile >> custarray[i].sHistory[0]; infile >> custarray[i].sHistory[1]; infile >> custarray[i].sHistory[2]; infile >> custarray[i].purchaseUnits; custarray[i].totalSales = custarray[i].sHistory[0] + custarray[i].sHistory[1] + custarray[i].sHistory[2]; i++; } i = i - 1; for(int a = 0; a < i; a++) { cout << custarray[a].first << '\t' << custarray[a].last << '\t' << custarray[a].state << '\t'; cout << custarray[a].sHistory[0] << " " << custarray[a].sHistory[1] << " " << custarray[a].sHistory[2]; cout << '\t' << custarray[a].purchaseUnits << '\t' << custarray[a].totalSales; cout << endl << endl; } }
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm> // for std::sort
#include <iomanip> // For output formatting
using namespace std;
class customer
{
public:
string first;
string last;
string state;
double sHistory[3]; // Sales history for three years.
double totalSales; // Total sales (adding all three years
together)
int purchaseUnits;
};
// Prototypes
void printcust(customer[], int);
void sortname(customer[], int);
void sortsales(customer[], int);
// Definitions
void printcust(customer custarray[], int n)
{
// Instead of using '\t', we could use setw()
// cout << "First\t" << "Last\t" << "State\t"
<< "SH 0 " << "SH 1 " << "SH 2 " << "Units
" << endl;
// for (int a = 0; a < n; a++)
// {
// cout << custarray[a].first << '\t' <<
custarray[a].last << '\t' << custarray[a].state
<< '\t';
// cout << custarray[a].sHistory[0] << " " <<
custarray[a].sHistory[1] << " " <<
custarray[a].sHistory[2];
// cout << '\t' << custarray[a].purchaseUnits <<
'\t' << custarray[a].totalSales;
// cout << endl << endl;
// }
cout << setw(15) << "First" << setw(15) <<
"Last" << setw(15) << "State" << setw(8) <<
"SH1"
<< setw(8) << "SH2" << setw(8) << "SH3"
<< setw(8) << "Units" << setw(8) << "Total"
<< endl << endl;
for (int a = 0; a < n; a++)
{
cout << setw(15) << custarray[a].first <<
setw(15) << custarray[a].last << setw(15) <<
custarray[a].state << setw(8)
<< custarray[a].sHistory[0] << setw(8) <<
custarray[a].sHistory[1] << setw(8) <<
custarray[a].sHistory[2]
<< setw(8) << custarray[a].purchaseUnits <<
setw(8) << custarray[a].totalSales << endl;
}
cout << endl << endl;
}
// A compare function is defined to compare the names and
// we use this compare function in std::sort()
bool compareNames(customer const& lhs, customer const&
rhs)
{
return lhs.last < rhs.last;
}
void sortname(customer custarray[], int n)
{
sort(custarray, custarray+n, &compareNames); // Specify first
and last address as well as the compare function
}
// Another compare function
bool compareSales(customer const& lhs, customer const&
rhs)
{
return lhs.totalSales > rhs.totalSales;
}
void sortsales(customer custarray[], int n)
{
sort(custarray, custarray+n, &compareSales);
}
int main()
{
fstream infile;
customer cust;
// Define size as a const so we only have to make changes at only
one place
const int size = 3;
customer custarray[size];
infile.open("data.txt");
int i = 0;
while (infile)
{
infile >> custarray[i].first;
infile >> custarray[i].last;
infile >> custarray[i].state;
infile >> custarray[i].sHistory[0];
infile >> custarray[i].sHistory[1];
infile >> custarray[i].sHistory[2];
infile >> custarray[i].purchaseUnits;
custarray[i].totalSales = custarray[i].sHistory[0] +
custarray[i].sHistory[1] + custarray[i].sHistory[2];
i++;
}
printcust(custarray, size);
cout << "----------------------------------Sorting by Last
Name-------------------------------" << endl;
sortname(custarray, size);
printcust(custarray, size);
cout << "----------------------------------Sorting by Total
Sales-----------------------------" << endl;
sortsales(custarray, size);
printcust(custarray, size);
// No need for this
// i = i - 1;
// Moved the following code to printcust()
// for (int a = 0; a < i; a++)
// {
// cout << custarray[a].first << '\t' <<
custarray[a].last << '\t' << custarray[a].state
<< '\t';
// cout << custarray[a].sHistory[0] << " " <<
custarray[a].sHistory[1] << " " <<
custarray[a].sHistory[2];
// cout << '\t' << custarray[a].purchaseUnits <<
'\t' << custarray[a].totalSales;
// cout << endl << endl;
// }
}
OUTPUT