Below is my code. I have an error that I still have difficulty figuring out. Please explain and
teach me the solution to fix it specifically (e.g. changing which line in the code). Thank you!
main.cpp
/*
Overloaded stream insertion operator <<
- used to display reports and write data to file.
Overloaded relational operator (<)
- used to sort the array in ascending order by name (insertion sort)
*/
#include "Sales.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int MAX_SIZE = 30;
/* Write your code here:
declare the function you are going to call in this program
*/
void readData(string fileName, Sales *salesArr, int &size);
void insertSort(Sales *salesArr, int size);
double calcSalesAvg(Sales *salesArr, int size);
void displayOverAvg(Sales *salesArr, int size, double avg);
void writeReport(Sales *salesArr, int size, string fileName);
void showReport(string fileName);
int main() {
Sales salesArr[MAX_SIZE];
int size = 0;
string fileName;
cout << "Please enter the input file's name: ";
getline(cin, fileName);
readData(fileName, salesArr, size);
insertSort(salesArr, size);
double avg = calcSalesAvg(salesArr, size);
displayOverAvg(salesArr, size, avg);
writeReport(salesArr, size, fileName);
string option;
cout << "Show report?" << endl;
getline(cin, option);
if (option == "Y" || option == "y")
showReport(fileName);
return 0;
}
// function definitions
void readData(string fileName, Sales *salesArr, int &size) {
string temp;
int i = 0;
fstream ptr;
ptr.open(fileName, ios::in);
while (getline(ptr, temp)) {
size++;
stringstream chk(temp);
string t2;
int id, year, amountSold;
string fname, lname;
int j = 0;
while (getline(chk, t2, ' ')) {
if (j == 0) {
id = stoi(t2);
}
if (j == 1) {
year = stoi(t2);
}
if (j == 2) {
fname = t2;
}
if (j == 3) {
lname = t2;
}
if (j == 4) {
amountSold = stoi(t2);
}
j++;
}
string gg = fname + " " + lname;
gg[gg.size() - 1] = 0;
Sales ss(id, year, gg, amountSold);
salesArr[i] = ss;
i++;
}
ptr.close();
}
void insertSort(Sales *salesArr, int size) {
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (salesArr[j] < salesArr[i]) {
Sales temp(salesArr[i]);
salesArr[i] = salesArr[j];
salesArr[j] = temp;
}
}
}
}
double calcSalesAvg(Sales *salesArr, int size) {
double d = 0;
for (int i = 0; i < size; i++) {
d += (salesArr[i].getAmountSold());
}
return (double) d / size;
}
void displayOverAvg(Sales *salesArr, int size, double avg) {
cout << "Average Sales: " << avg << endl;
string nm ;
cout << "Salespeople with above average sales:" << endl;
for (int i = 0; i < size; i++) {
if (salesArr[i].getAmountSold() > avg) {
cout << salesArr[i];
}
}
}
void writeReport(Sales *salesArr, int size, string fileName) {
fileName.insert(fileName.find("."), "Report");
fstream ptr;
ptr.open(fileName, ios::out);
for (int i = 0; i < size; i++) {
ptr << salesArr[i];
}
ptr.close();
}
/*
This function receives the name of a file and
displays its contents to the screen.
*/
void showReport(string fileName)
{
fileName.insert(fileName.find("."), "Report");
ifstream in(fileName);
if (in.fail())
{
cout << "Input file: " << fileName << " not found!" << endl;
exit(EXIT_FAILURE);
}
string line;
while (getline(in, line))
{
cout << line << endl;
}
in.close();
}
Sales.h
/*
Specification file for the Sales class
- Overloaded stream insertion operator (<<)
- Overloaded relational operator (<)
*/
#ifndef SALES_H
#define SALES_H
#include <string>
using std::ostream;
using std::string;
// ^^^ avoid adding using namespace std;
class Sales {
private:
int id;
int year;
string name;
int amountSold;
public:
// constructors
Sales();
Sales(int i, int y, string n, int a);
Sales(Sales &s);
// getters
int getId();
int getYear();
string getName();
int getAmountSold();
// setters
void setId(int);
void setYear(int);
void setName(string);
void setAmountSold(int);
// overloaded operators
// Overloaded <
bool operator< (Sales &b);
// Overloaded <<
friend ostream& operator<< (ostream &out, Sales &b);
};
#endif
Sales.cpp
/*
Implementation file for the Sales class.
*/
#include "Sales.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// default constructor; setting everything to 0 or ""
Sales::Sales() {
id = 0;
year = 0;
name = "";
amountSold = 0;
}
// overloaded constructor; setting the variables according to the parameters
Sales::Sales(int i, int y, string n, int a) {
id = i ;
year = y;
name = n;
amountSold = a;
}
Sales::Sales(Sales &s) {
id = s.getId();
year = s.getYear();
name = s.getName();
amountSold = s.getAmountSold();
}
// getter and setter
int Sales::getId() {
return id;
}
void Sales::setId(int i) {
id = i;
}
int Sales::getYear() {
return year;
}
void Sales::setYear(int y) {
year = y;
}
string Sales::getName() {
return name;
}
void Sales::setName(string n) {
name = n;
}
int Sales::getAmountSold() {
return amountSold;
}
void Sales::setAmountSold(int a) {
amountSold = a;
}
bool Sales::operator< (Sales &b) {
if (this->getName().compare(b.getName()) <= 0)
return true;
return false;
}
ostream& operator<<(ostream &out, Sales &b) {
out << setw(20) << left << b.getName();
out << setw(9) << right << b.getAmountSold();
out << setw(13) << right << rand()%500;
out << endl;
return out;
}
Sales.txt
13492785 2017 North, Jane; 1000
78534520 2012 South, Tim; 950
20192756 2017 East, Linda; 15000
19273458 2012 West, Paul; 5000
78520192 2017 Doe, Mary Jane; 5001
32278520 2012 Smith, Victor; 7995
14278520 2012 Johnson, Mary; 120
56192785 2017 Baker, Tom; 1300
88278529 2012 Newman, Diana; 1500
89278527 2012 Peterson, William; 14200
98278528 2012 Gaddis, Jim; 1200
99192785 2017 King, Laura; 1000
43278524 2012 McDonald, Ann; 2000
88288522 2013 Newman, Dan; 5500
newSales.txt
13491995 2015 Davis, Andrew; 9125
78531280 2014 Potter, Monica T.; 9125
22112756 2013 Lucas, George Paul; 9125
76573458 2011 Pan, Peter; 9125
7.15 Lab: Sales Class - Array of Sales objects (overload operators) Reuse the sales class with the
following modifications: - Overload the stream insertion operator ( << ) and use it to display
reports and write data to file using the same format (see below). - Overload the relational
operator ( < ) and use it to sort the array in ascending order by name (insertion sort) This
program will create an array of 30 Sales objects and it will read data from an input file (Sales.txt)
into this array. It will sort the array in ascending order by name. Then it will calculate the
average of all sales people and display on the screen the average sale followed by the names of
the salespeople with above average sales, the amount sold and the amount earned. If the list is
empty, write "N/A" as shown below: Average Sales: $9125 Salespeople with above average
sales: N / A Finally, it will write to another file (salesReport.txt) a table as shown below: Display
the amount earned with 2 decimals (out setprecision(2) fixed; ). Assume that a name has at most
20 characters (for formatting). Prompt the user to enter the name of the input file (with
extension). Generate the name of the output file by adding the word "Report" to the input file's
name. If the input file name is sales.txt, the output file name will be salesReport.txt If the input
file name is newSales.txt, the output file name will be newSalesReport.txt Display the output
file's name as shown below: Program errors displayed here Exited with return code -6
(SIGABRT). terminate called after throwing an instance of 'std::invalid_argument' what (): stoi
Program output displayed here Please enter the input file's name:

Below is my code- I have an error that I still have difficulty figurin.pdf

  • 1.
    Below is mycode. I have an error that I still have difficulty figuring out. Please explain and teach me the solution to fix it specifically (e.g. changing which line in the code). Thank you! main.cpp /* Overloaded stream insertion operator << - used to display reports and write data to file. Overloaded relational operator (<) - used to sort the array in ascending order by name (insertion sort) */ #include "Sales.h" #include <iostream> #include <sstream> #include <iomanip> #include <fstream> #include <string> using namespace std; const int MAX_SIZE = 30; /* Write your code here: declare the function you are going to call in this program */ void readData(string fileName, Sales *salesArr, int &size); void insertSort(Sales *salesArr, int size); double calcSalesAvg(Sales *salesArr, int size); void displayOverAvg(Sales *salesArr, int size, double avg); void writeReport(Sales *salesArr, int size, string fileName); void showReport(string fileName); int main() { Sales salesArr[MAX_SIZE]; int size = 0; string fileName; cout << "Please enter the input file's name: "; getline(cin, fileName); readData(fileName, salesArr, size); insertSort(salesArr, size); double avg = calcSalesAvg(salesArr, size); displayOverAvg(salesArr, size, avg); writeReport(salesArr, size, fileName);
  • 2.
    string option; cout <<"Show report?" << endl; getline(cin, option); if (option == "Y" || option == "y") showReport(fileName); return 0; } // function definitions void readData(string fileName, Sales *salesArr, int &size) { string temp; int i = 0; fstream ptr; ptr.open(fileName, ios::in); while (getline(ptr, temp)) { size++; stringstream chk(temp); string t2; int id, year, amountSold; string fname, lname; int j = 0; while (getline(chk, t2, ' ')) { if (j == 0) { id = stoi(t2); } if (j == 1) { year = stoi(t2); } if (j == 2) { fname = t2; } if (j == 3) { lname = t2; } if (j == 4) { amountSold = stoi(t2); } j++; }
  • 3.
    string gg =fname + " " + lname; gg[gg.size() - 1] = 0; Sales ss(id, year, gg, amountSold); salesArr[i] = ss; i++; } ptr.close(); } void insertSort(Sales *salesArr, int size) { for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (salesArr[j] < salesArr[i]) { Sales temp(salesArr[i]); salesArr[i] = salesArr[j]; salesArr[j] = temp; } } } } double calcSalesAvg(Sales *salesArr, int size) { double d = 0; for (int i = 0; i < size; i++) { d += (salesArr[i].getAmountSold()); } return (double) d / size; } void displayOverAvg(Sales *salesArr, int size, double avg) { cout << "Average Sales: " << avg << endl; string nm ; cout << "Salespeople with above average sales:" << endl; for (int i = 0; i < size; i++) { if (salesArr[i].getAmountSold() > avg) { cout << salesArr[i]; } } } void writeReport(Sales *salesArr, int size, string fileName) { fileName.insert(fileName.find("."), "Report"); fstream ptr; ptr.open(fileName, ios::out);
  • 4.
    for (int i= 0; i < size; i++) { ptr << salesArr[i]; } ptr.close(); } /* This function receives the name of a file and displays its contents to the screen. */ void showReport(string fileName) { fileName.insert(fileName.find("."), "Report"); ifstream in(fileName); if (in.fail()) { cout << "Input file: " << fileName << " not found!" << endl; exit(EXIT_FAILURE); } string line; while (getline(in, line)) { cout << line << endl; } in.close(); } Sales.h /* Specification file for the Sales class - Overloaded stream insertion operator (<<) - Overloaded relational operator (<) */ #ifndef SALES_H #define SALES_H #include <string> using std::ostream; using std::string; // ^^^ avoid adding using namespace std; class Sales { private: int id;
  • 5.
    int year; string name; intamountSold; public: // constructors Sales(); Sales(int i, int y, string n, int a); Sales(Sales &s); // getters int getId(); int getYear(); string getName(); int getAmountSold(); // setters void setId(int); void setYear(int); void setName(string); void setAmountSold(int); // overloaded operators // Overloaded < bool operator< (Sales &b); // Overloaded << friend ostream& operator<< (ostream &out, Sales &b); }; #endif Sales.cpp /* Implementation file for the Sales class. */ #include "Sales.h" #include <iostream> #include <iomanip> #include <string> using namespace std; // default constructor; setting everything to 0 or "" Sales::Sales() { id = 0;
  • 6.
    year = 0; name= ""; amountSold = 0; } // overloaded constructor; setting the variables according to the parameters Sales::Sales(int i, int y, string n, int a) { id = i ; year = y; name = n; amountSold = a; } Sales::Sales(Sales &s) { id = s.getId(); year = s.getYear(); name = s.getName(); amountSold = s.getAmountSold(); } // getter and setter int Sales::getId() { return id; } void Sales::setId(int i) { id = i; } int Sales::getYear() { return year; } void Sales::setYear(int y) { year = y; } string Sales::getName() { return name; } void Sales::setName(string n) { name = n; }
  • 7.
    int Sales::getAmountSold() { returnamountSold; } void Sales::setAmountSold(int a) { amountSold = a; } bool Sales::operator< (Sales &b) { if (this->getName().compare(b.getName()) <= 0) return true; return false; } ostream& operator<<(ostream &out, Sales &b) { out << setw(20) << left << b.getName(); out << setw(9) << right << b.getAmountSold(); out << setw(13) << right << rand()%500; out << endl; return out; } Sales.txt 13492785 2017 North, Jane; 1000 78534520 2012 South, Tim; 950 20192756 2017 East, Linda; 15000 19273458 2012 West, Paul; 5000 78520192 2017 Doe, Mary Jane; 5001 32278520 2012 Smith, Victor; 7995 14278520 2012 Johnson, Mary; 120 56192785 2017 Baker, Tom; 1300 88278529 2012 Newman, Diana; 1500 89278527 2012 Peterson, William; 14200 98278528 2012 Gaddis, Jim; 1200 99192785 2017 King, Laura; 1000 43278524 2012 McDonald, Ann; 2000 88288522 2013 Newman, Dan; 5500 newSales.txt 13491995 2015 Davis, Andrew; 9125 78531280 2014 Potter, Monica T.; 9125 22112756 2013 Lucas, George Paul; 9125 76573458 2011 Pan, Peter; 9125
  • 8.
    7.15 Lab: SalesClass - Array of Sales objects (overload operators) Reuse the sales class with the following modifications: - Overload the stream insertion operator ( << ) and use it to display reports and write data to file using the same format (see below). - Overload the relational operator ( < ) and use it to sort the array in ascending order by name (insertion sort) This program will create an array of 30 Sales objects and it will read data from an input file (Sales.txt) into this array. It will sort the array in ascending order by name. Then it will calculate the average of all sales people and display on the screen the average sale followed by the names of the salespeople with above average sales, the amount sold and the amount earned. If the list is empty, write "N/A" as shown below: Average Sales: $9125 Salespeople with above average sales: N / A Finally, it will write to another file (salesReport.txt) a table as shown below: Display the amount earned with 2 decimals (out setprecision(2) fixed; ). Assume that a name has at most 20 characters (for formatting). Prompt the user to enter the name of the input file (with extension). Generate the name of the output file by adding the word "Report" to the input file's name. If the input file name is sales.txt, the output file name will be salesReport.txt If the input file name is newSales.txt, the output file name will be newSalesReport.txt Display the output file's name as shown below: Program errors displayed here Exited with return code -6 (SIGABRT). terminate called after throwing an instance of 'std::invalid_argument' what (): stoi Program output displayed here Please enter the input file's name: