Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help with errors and i used g++ sales.cpp main.cpp -o sales please help and

ID: 3591597 • Letter: N

Question

need help with errors and i used g++ sales.cpp main.cpp -o sales please help and guide me what g++ should i use

#ifndef SALES_H_INCLUDED
#define SALES_H_INCLUDED
#include <string>
using namespace std;
struct slip
{
int SalesPersonID;
int ProductID;
string SaleDate;
double TotalSales; //dollar value of product sold
};
class sales
{
slip DailySlips[620]; //this array stores slips submitted by each salesperson each day for a month
/*There are 4 salespersons, if each submits 5 slips everyday for a maximum of 31 days, the number of
slips submitted for that month will be 4*5*31=620. So we make room for the maximum possible number of slips
even if all the space in the array will be rarely utilized*/
double summary[4][5]; //2D array storing the summary of the previous 1D array
/*The four rows represent the 4 salespersons and the five columns, the 5 products.
Each entry in this array is the sales in dollar amount. For example, the first entry [0][0] is the amount
of sales made by the 1st salesperson for the first product, entry [1][0] is the sales made by the 2nd salesperson
for the 1st product and so on*/
string SalesPersons[4]; //names of the 4 salespersons
string Products[5]; //names of the 5 products
public:
sales(); //default constructor, this will be used to initialize the 2D array values to 0 & fill the other 1D arrays
void summarize(); //this will fill the 2D array, summary with data from the DailySlips array
void inputSales(); //input sales details into the DailySlips array
double sumSales(int SalesPersonID,int ProdID); //calculate total sales to fill the 2D array
/*this method takes the Salesperson ID and Product ID as arguments
and returns the total sales made by that salesperson on that product for the month*/
void display(); //show the data in the required table format
};

#endif

#include<iostream>
#include<string>
#include<iomanip>

#include "sales.h"

using namespace std;
sales::sales() //default constructor
{
//fill the SalesPersons array
string firstName,lastName; //first and last name of the salespersons
cout<<"Enter the names of the 4 salespersons :"<<endl;
for(int i=0;i<4;i++)
{
cout<<"Salesperson "<<i+1<<":-"<<endl;
cout<<"First Name : ";
cin>>firstName;
cout<<"Last Name : ";
cin>>lastName;
SalesPersons[i]=firstName+" "+lastName;
}
fflush(stdin); //clear the input stream
//now fill the Products array
string ProdName;
cout<<" Enter the names of the 5 products :"<<endl;
for(int i=0;i<5;i++)
{
cout<<"Product "<<i+1<<": ";
getline(cin,ProdName); //use this instead of cin>> to input spaces between words
Products[i]=ProdName;
}
//now initialize the 2D array to 0
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
summary[i][j]=0;
//now, we initialize the SalesPersonID in each struct of the array DailySlips[] to -1. This will help
//in determining the number of actual slips stored in the array later
for(int i=0;i<620;i++)
DailySlips[i].SalesPersonID=-1;
}
void sales::inputSales() //input the daily slips data
{
char ch='y'; //to check when you want to stop entering data
int i=0; //index for the array
cout<<" Enter the data on the slips :"<<endl;
while(ch=='y' || ch=='Y') //keep looping as long as ch equals y or Y
{
cout<<"Salesperson ID : ";
cin>>DailySlips[i].SalesPersonID;
cout<<"Product ID : ";
cin>>DailySlips[i].ProductID;
cout<<"Sale Date : ";
cin>>DailySlips[i].SaleDate;
cout<<"Total Sales : ";
cin>>DailySlips[i].TotalSales;
cout<<" Enter another slip?[y/n] : ";
cin>>ch;
i++; //increment the index
}
}
double sales::sumSales(int SalesPersonID,int ProdID) //calculate total sales by salesperson on a product
{
int i=0; //array index
double sum=0; //the total sales to be calculated
while(DailySlips[i].SalesPersonID!=-1) //loop thru all valid entries of the 1D array
{
if(DailySlips[i].ProductID==ProdID && DailySlips[i].SalesPersonID==SalesPersonID)
sum+=DailySlips[i].TotalSales; //add the total sales on the slip into the sum
i++; //increment the array index
}
return sum; //return the calculated sum
}
void sales::summarize() //fill the 2D array with the total sales data
{
for(int i=0;i<4;i++)
for(int j=0;j<5;j++)
summary[i][j]=sumSales(i+1,j+1); //pass the salesperson & product IDs to sumSales() and save the returned value
//in the 2D array
}
void sales::display() //show the output in the required format
{
double subTotal,grandTotal=0;
cout<<"DOODAD THIRD QUARTER 2016 SALES REPORT"<<endl;
cout<<setw(25)<<left<<"SALES PERSON"<<" "<<setw(25)<<left<<"PRODUCT"<<" "<<setw(15)<<right<<"AMOUNT"<<" "<<setw(15)<<right<<"TOTAL";
cout<<endl<<setw(25)<<"-------------------------"<<" "<<setw(25)<<"-------------------------"<<" "<<setw(15)<<"---------------"<<" "<<setw(15)<<"---------------"<<endl;
for(int i=0;i<4;i++)
{
subTotal=0;
for(int j=0;j<5;j++)
{
if(summary[i][j]!=0) //if sales were made by this salesperson for this product
{
subTotal+=summary[i][j];
cout<<setw(25)<<left<<SalesPersons[i]<<" "<<setw(25)<<left<<Products[j]<<" "<<setw(15)<<right<<summary[i][j];
}
if(j<4 && summary[i][j+1]==0)
continue;
if(subTotal!=0) //display subtotal and go to next line only if sales have been made
{
if(j==4) //if this is the last column for the salesman
cout<<" "<<setw(15)<<right<<subTotal<<endl; //display the subtotal in the next column
else
cout<<endl; //else go to the next line
}
}
cout<<endl; //leave a blank line before the next salesperson
grandTotal+=subTotal; //update the grand total
}
cout<<setw(68)<<" GRAND TOTAL OF ALL SALES"<<setw(15)<<grandTotal<<endl; //show the grand total
}

#include <iostream>
#include "sales.cpp"
using namespace std;
int main()
{
sales S; //create a sales object (will call the constructor)
S.inputSales(); //input the daily slips data
S.summarize(); //fill the 2D array
S.display(); //show the output table
}

In file included from main.cpp:2:0: sales . cpp: In constructor 'sales : : sales ( ) , . sales.cpp:21:8: error: stdin was not declared in this scope sales.cpp: 21:13: error 'fflush' was not declared in this scope sales.cpp: In constructor 'sales::sales)' sales.cpp:21:8: error: stdin was not declared in this scope sales.cpp: 21:13: error 'fflush' was not declared in this scope

Explanation / Answer

There were some issues in your code, I have fixed them

sales.cpp - It was missing includion of cstdio library which is required for fflush(stdin)

#include<iostream>
#include<string>
#include<iomanip>
#include <cstdio>
#include "sales.h"
using namespace std;
sales::sales() //default constructor
{
//fill the SalesPersons array
string firstName,lastName; //first and last name of the salespersons
cout<<"Enter the names of the 4 salespersons :"<<endl;
for(int i=0;i<4;i++)
{
cout<<"Salesperson "<<i+1<<":-"<<endl;
cout<<"First Name : ";
cin>>firstName;
cout<<"Last Name : ";
cin>>lastName;
SalesPersons[i]=firstName+" "+lastName;
}
fflush(stdin); //clear the input stream
//now fill the Products array
string ProdName;
cout<<" Enter the names of the 5 products :"<<endl;
for(int i=0;i<5;i++)
{
cout<<"Product "<<i+1<<": ";
getline(cin,ProdName); //use this instead of cin>> to input spaces between words
Products[i]=ProdName;
}
//now initialize the 2D array to 0
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
summary[i][j]=0;
//now, we initialize the SalesPersonID in each struct of the array DailySlips[] to -1. This will help
//in determining the number of actual slips stored in the array later
for(int i=0;i<620;i++)
DailySlips[i].SalesPersonID=-1;
}
void sales::inputSales() //input the daily slips data
{
char ch='y'; //to check when you want to stop entering data
int i=0; //index for the array
cout<<" Enter the data on the slips :"<<endl;
while(ch=='y' || ch=='Y') //keep looping as long as ch equals y or Y
{
cout<<"Salesperson ID : ";
cin>>DailySlips[i].SalesPersonID;
cout<<"Product ID : ";
cin>>DailySlips[i].ProductID;
cout<<"Sale Date : ";
cin>>DailySlips[i].SaleDate;
cout<<"Total Sales : ";
cin>>DailySlips[i].TotalSales;
cout<<" Enter another slip?[y/n] : ";
cin>>ch;
i++; //increment the index
}
}
double sales::sumSales(int SalesPersonID,int ProdID) //calculate total sales by salesperson on a product
{
int i=0; //array index
double sum=0; //the total sales to be calculated
while(DailySlips[i].SalesPersonID!=-1) //loop thru all valid entries of the 1D array
{
if(DailySlips[i].ProductID==ProdID && DailySlips[i].SalesPersonID==SalesPersonID)
sum+=DailySlips[i].TotalSales; //add the total sales on the slip into the sum
i++; //increment the array index
}
return sum; //return the calculated sum
}
void sales::summarize() //fill the 2D array with the total sales data
{
for(int i=0;i<4;i++)
for(int j=0;j<5;j++)
summary[i][j]=sumSales(i+1,j+1); //pass the salesperson & product IDs to sumSales() and save the returned value
//in the 2D array
}
void sales::display() //show the output in the required format
{
double subTotal,grandTotal=0;
cout<<"DOODAD THIRD QUARTER 2016 SALES REPORT"<<endl;
cout<<setw(25)<<left<<"SALES PERSON"<<" "<<setw(25)<<left<<"PRODUCT"<<" "<<setw(15)<<right<<"AMOUNT"<<" "<<setw(15)<<right<<"TOTAL";
cout<<endl<<setw(25)<<"-------------------------"<<" "<<setw(25)<<"-------------------------"<<" "<<setw(15)<<"---------------"<<" "<<setw(15)<<"---------------"<<endl;
for(int i=0;i<4;i++)
{
subTotal=0;
for(int j=0;j<5;j++)
{
if(summary[i][j]!=0) //if sales were made by this salesperson for this product
{
subTotal+=summary[i][j];
cout<<setw(25)<<left<<SalesPersons[i]<<" "<<setw(25)<<left<<Products[j]<<" "<<setw(15)<<right<<summary[i][j];
}
if(j<4 && summary[i][j+1]==0)
continue;
if(subTotal!=0) //display subtotal and go to next line only if sales have been made
{
if(j==4) //if this is the last column for the salesman
cout<<" "<<setw(15)<<right<<subTotal<<endl; //display the subtotal in the next column
else
cout<<endl; //else go to the next line
}
}
cout<<endl; //leave a blank line before the next salesperson
grandTotal+=subTotal; //update the grand total
}
cout<<setw(68)<<" GRAND TOTAL OF ALL SALES"<<setw(15)<<grandTotal<<endl; //show the grand total
}

main.cpp - We should include .h file and not .cpp file in our code.

#include <iostream>
#include "sales.h"
using namespace std;
int main()
{
sales S; //create a sales object (will call the constructor)
S.inputSales(); //input the daily slips data
S.summarize(); //fill the 2D array
S.display(); //show the output table
}

sales.h

#ifndef SALES_H_INCLUDED
#define SALES_H_INCLUDED
#include <string>
using namespace std;
struct slip
{
int SalesPersonID;
int ProductID;
string SaleDate;
double TotalSales; //dollar value of product sold
};
class sales
{
slip DailySlips[620]; //this array stores slips submitted by each salesperson each day for a month
/*There are 4 salespersons, if each submits 5 slips everyday for a maximum of 31 days, the number of
* slips submitted for that month will be 4*5*31=620. So we make room for the maximum possible number of slips
* even if all the space in the array will be rarely utilized*/
double summary[4][5]; //2D array storing the summary of the previous 1D array
/*The four rows represent the 4 salespersons and the five columns, the 5 products.
* Each entry in this array is the sales in dollar amount. For example, the first entry [0][0] is the amount
* of sales made by the 1st salesperson for the first product, entry [1][0] is the sales made by the 2nd salesperson
* for the 1st product and so on*/
string SalesPersons[4]; //names of the 4 salespersons
string Products[5]; //names of the 5 products

public:
sales(); //default constructor, this will be used to initialize the 2D array values to 0 & fill the other 1D arrays
void summarize(); //this will fill the 2D array, summary with data from the DailySlips array
void inputSales(); //input sales details into the DailySlips array
double sumSales(int SalesPersonID,int ProdID); //calculate total sales to fill the 2D array
/*this method takes the Salesperson ID and Product ID as arguments
* and returns the total sales made by that salesperson on that product for the month*/
void display(); //show the data in the required table format
};
#endif

Command to compile

g++ sales.cpp main.cpp -o sales

this will give you an executable with name sales