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 s.
Below is my code- I have an error that I still have difficulty figurin.pdf
1. 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);
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;
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;
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;
}
8. 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: