Question

In: Computer Science

I have to create a program that: define class cust(I already did this part) create an...

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. 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; 
}
    }

Solutions

Expert Solution

#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


Related Solutions

Part I: Have a program class named TestArrays This class should have a main() method that...
Part I: Have a program class named TestArrays This class should have a main() method that behaves as follows: Have an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
Good morning, Yes I did post part a Someone answered part (a) and (c) already. Here...
Good morning, Yes I did post part a Someone answered part (a) and (c) already. Here is the answered part (A). Can you complete part B please Thank you Here is part (A) again Comprehensive Problem 5 Part A: Note: You must complete part A before completing parts B and C. Genuine Spice Inc. began operations on January 1 of the current year. The company produces 8-ounce bottles of hand and body lotion called Eternal Beauty. The lotion is sold...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called Dog that contains instance data that represent the dog's name and age. define the Dog constructor to accept and initialize instance data. create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age). Include a...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A In late 2020, the Nicklaus Corporation was formed. The corporate charter authorizes the issuance of 6,000,000 shares of common stock carrying a $1 par value, and 2,000,000 shares of $5 par value, noncumulative, nonparticipating preferred stock. On January 2, 2021, 4,000,000 shares of the common stock are issued in exchange for cash at an average price of $10 per share. Also on January 2,...
Using Java This is two-part question, but I have already completed the first part and just...
Using Java This is two-part question, but I have already completed the first part and just need help with the second. Here I will provide both questions and my answer to the first part: Part I Question: Write a class called Dog that contains instance data that represents the dog’s name, breed, weight, birthdate, and medical history. Define the Dog constructor to accept and initialize instance data (begin the medical history with an empty line). Include accessor and mutator methods...
Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
I already have a database in phpMyAdmin and need to create an html/php "SEARCH" page to...
I already have a database in phpMyAdmin and need to create an html/php "SEARCH" page to retrieve the information from the database on to my localhost. Please post a php/html code that will create a search page and return the data from my phpmyadmin database. The 3 items I have on the database are first_name, last_name and birth_day.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT