In: Computer Science
Hi, could you please expand on this code to include: Total earnings per division (East, West, North and south); Highest and lowest per division (East, West, North and South) and total earning for the entire company. Thank you so very much! Lab12B.cpp ========= #include #include using namespace std; //program to prompt user for sales of any division in any quarter and save to binary file struct Division { char name; //values could be N, E , W or S int quarter; //values could be 1, 2, 3, 4 double sales; }; int main() { ifstream inFile("divisions.dat", ios::in | ios::binary); string ans = "y"; Division div; if(inFile.fail()) { cout << "Could not open divisions.dat for reading. Please use Lab12A to create the file" << endl; return 0; } cout << "Reading data from divisions.dat" << endl; inFile.read(reinterpret_cast(&div), sizeof(div)); while(!inFile.eof()) { cout << "Division name: " << div.name << endl; cout << "Quarter No.: " << div.quarter << endl; cout << "Sales amount: " << div.sales << endl << endl; inFile.read(reinterpret_cast(&div), sizeof(div)); } inFile.close(); }
Solution : Added code is in the bold text.
#include<iostream>
#define MAX 100000
using namespace std; //program to prompt user for sales of any
division in any quarter and save to binary file
struct Division {
char name; //values could be N, E , W or S
int quarter; //values could be 1, 2, 3, 4
double sales;
};
int main()
{
//variable declaration
double totalEarnEAST=0;
double lowestEarnEAST=MAX;
double highestEarnEAST=0;
double totalEarnWEST=0;
double lowestEarnWEST=MAX;
double highestEarnWEST=0;
double totalEarnNORTH=0;
double lowestEarnNORTH=MAX;
double highestEarnNORTH=0;
double totalEarnSOUTH=0;
double lowestEarnSOUTH=MAX;
double highestEarnSOUTH=0;
ifstream inFile("divisions.dat", ios::in | ios::binary);
string ans = "y";
Division div;
if(inFile.fail())
{ cout << "Could not open divisions.dat for reading. Please
use Lab12A to create the file" << endl;
return 0;
}
cout << "Reading data from divisions.dat" <<
endl;
inFile.read(reinterpret_cast(&div), sizeof(div));
while(!inFile.eof())
{
cout << "Division name: " << div.name <<
endl;
cout << "Quarter No.: " << div.quarter <<
endl;
cout << "Sales amount: " << div.sales << endl
<< endl;
//set the value total ,lowest , highest earning division
wise.
switch(div.name)
{
case 'E':
totalEarnEAST+=div.sales;
if(highestEarnEAST<div.sales)
highestEarnEAST=div.sales;
if(lowestEarnEAST>div.sales)
lowestEarnEAST=div.sales;
break;
case 'W':
totalEarnWEST+=div.sales;
if(highestEarnWEST<div.sales)
highestEarnWEST=div.sales;
if(lowestEarnWESTdiv.sales)
lowestEarnWEST=div.sales;
break;
case 'N':
totalEarnEAST+=div.sales;
if(highestEarnNORTH<div.sales)
highestEarnNORTH=div.sales;
if(lowestEarnNORTH>div.sales)
lowestEarnNORTH=div.sales;
break;
case 'S':
totalEarnEAST+=div.sales;
if(highestEarnSOUTH<div.sales)
highestEarnSOUTH=div.sales;
if(lowestEarnSOUTH>div.sales)
lowestEarnSOUTH=div.sales;
break;
}
inFile.read(reinterpret_cast(&div), sizeof(div));
}
inFile.close();
//printing the values
cout<<"Total earning in EAST
Division:"<<totalEarnEAST<<endl;
cout<<"Lowest earning in EAST
Division:"<<lowestEarnEAST<<endl;
cout<<"Highest earning in EAST
Division:"<<highestEarnEAST<<endl;
cout<<"Total earning in WEST
Division:"<<totalEarnWEST<<endl;
cout<<"Lowest earning in WEST
Division:"<<lowestEarnWEST<<endl;
cout<<"Highest earning in WEST
Division:"<<highestEarnWEST<<endl;
cout<<"Total earning in NORTH
Division:"<<totalEarnNORTH<<endl;
cout<<"Lowest earning in NORTH
Division:"<<lowestEarnNORTH<<endl;
cout<<"Highest earning in NORTH
Division:"<<highestEarnNORTH<<endl;
cout<<"Total earning in SOUTH
Division:"<<totalEarnSOUTH<<endl;
cout<<"Lowest earning in SOUTH
Division:"<<lowestEarnSOUTH<<endl;
cout<<"Highest earning in SOUTH
Division:"<<highestEarnSOUTH<<endl;
}