/* Corporate Sales Data Output - This program uses a structure to store
the following data on a company division:
* Division Name: East, West, North, South
* Quarter: 1, 2, 3, 4
* Quarterly Sales
The user is asked for the four quarters' sales figures for the East,
West, North and South divisions. The data for each quarter for each
division is written to a file.
Input Validation: No negative numbers for sales figures are accepted. */
#include "Utility.h"
const int NAME_SIZE = 15;
const int NUM_BRANCHES = 4;
const int NUM_QUARTERS = 4;
struct CorporateSales
{
char divisionName[NAME_SIZE]; /* Holds the division names */
array<double, NUM_BRANCHES> quarterlySales; /* Array to hold the quarterly sales figures*/
};
struct DivisionSales
{
CorporateSales sales[NUM_BRANCHES];
/* Initializer list */
DivisionSales() : sales{ { "North Branch", { 0.0 } }, { "South Branch", { 0.0 } },
{ "East Branch", { 0.0 } }, { "West Branch", { 0.0} } } {}
};
void getSalesData(DivisionSales &);
int writeData(DivisionSales &);
int main()
{
DivisionSales division;
getSalesData(division);
writeData(division);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getSalesData
This function uses a reference parameter to a structure as
its argument. The user is asked to enter quarterly sales
figures for the four branches of a company. This data is
stored in the appropriate structure member variables.
********************************************************** */
void getSalesData(DivisionSales &division)
{
cout << "\n\tISHIKAWA FRUIT COMPANY - SALES DATA SYSTEM\n\n";
for (int index = 0; index < NUM_BRANCHES; index++)
{
cout << "\n\tEnter Sales Data for " << division.sales[index].divisionName << ":\n";
for (int qtrs = 0; qtrs < NUM_QUARTERS; qtrs++)
{
cout << "\tQuarter " << (qtrs + 1) << ":\t$ ";
cin >> division.sales[index].quarterlySales[qtrs];
while (division.sales[index].quarterlySales[qtrs] <= 0.0)
{
cout << "\tQuarter " << (qtrs + 1) << ":\t$ ";
cin >> division.sales[index].quarterlySales[qtrs];
}
}
}
}
/* **********************************************************
Definition: writeData
This function uses a reference variable to a structure as
its argument. The user is asked to enter a filename. Upon
success, the CorporateSales structure data is written to
to disk. In case of an error a message is displayed, and
the program exits.
********************************************************** */
int writeData(DivisionSales &division)
{
string fileName = "";
cout << "\n\tEnter name of the file to output sales data to: ";
cin >> fileName;
fstream salesData(fileName.c_str(), ios::out | ios::binary);
if (!salesData.fail())
{
cout << "\n\tWriting " << fileName << " to disk ...\n";
salesData.write(reinterpret_cast<const char *>(&division),
sizeof(CorporateSales) * NUM_BRANCHES);
cout << "\n\tData successfully written to " << fileName << "\n"
<< "\tNow closing the program ...\n\n"
<< "\tISHIKAWA FRUIT COMPANY - Your number one fruits supplier!";
}
else
{
cout << "\n\tFile Write Error! Could not write to " << fileName << "\n"
<< "\tPlease close this program and try again ...";
return -1;
}
salesData.close();
return 0;
}
No comments:
Post a Comment