I have to create a program that: - define class cust(I already did this part) - create an array of cust (size 10)to hold the data of the class. (done) - read the date from a file(everything from the class - already done) - call a function print cust(this function will print all the data in table format)....first, last, state, sales history(0,1,2), units - Read the data from a file (you may break up the strings and numeric values any way you choose (separate lines may be easier) - Make a function call to a function called printcust. This function will print all the data for all of the customers (in tabular format). - Make a function call to a function called sortname which will sort the cust array alphabetically by last name. - Make a function call to the function printcust. - Make a function call to a function called sortsales. This function will sort the above array in descending order of the total sales. - Call the function printcust
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.
#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;
}
}
Aucun commentaire:
Enregistrer un commentaire