//Sam Partovi CS1A Ch. 11, P.645, #3
/*******************************************************************************
* STORE CORPORATE SALES DATA
* ____________________________________________________________
* This program accepts corporate sales data and stores it, calculating averages
* where necessary.
* ____________________________________________________________
*INPUT
* firstQuarterSales : Sales in first quarter
* secondQuarterSales : Sales in second quarter
* thirdQuarterSales : Sales in third quarter
* fourthQuarterSales : Sales in fourth quarter
*OUTPUT
* totalAnnualSales : Accumulated yearly sales
* averageQuarterlySales : Average of quarterly sales
*******************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Structure to hold division sales data
struct Division {
string name;
double firstQuarterSales;
double secondQuarterSales;
double thirdQuarterSales;
double fourthQuarterSales;
double totalAnnualSales;
double averageQuarterlySales;
};
// Function prototypes
void getSalesData(Division& division);
void calculateSales(Division& division);
void displayDivisionData(const Division& division);
void validateSales(double& sales);
int main() {
// Create four division variables
Division east = {"East"};
Division west = {"West"};
Division north = {"North"};
Division south = {"South"};
// Input sales data for each division
cout << "\nEnter sales data for the East division:";
getSalesData(east);
cout << "\nEnter sales data for the West division:";
getSalesData(west);
cout << "\nEnter sales data for the North division:";
getSalesData(north);
cout << "\nEnter sales data for the South division:";
getSalesData(south);
// Calculate totals and averages
calculateSales(east);
calculateSales(west);
calculateSales(north);
calculateSales(south);
// Display sales data
cout << fixed << setprecision(2);
cout << "\nCorporate Sales Data\n";
displayDivisionData(east);
displayDivisionData(west);
displayDivisionData(north);
displayDivisionData(south);
return 0;
}
/*******************************************************************************
* getSalesData
* Prompts the user to input the sales data for each quarter of a division.
******************************************************************************/
void getSalesData(Division& division) {
cout << "\nEnter sales for the first quarter: ";
cin >> division.firstQuarterSales;
validateSales(division.firstQuarterSales);
cout << "\nEnter sales for the second quarter: ";
cin >> division.secondQuarterSales;
validateSales(division.secondQuarterSales);
cout << "\nEnter sales for the third quarter: ";
cin >> division.thirdQuarterSales;
validateSales(division.thirdQuarterSales);
cout << "\nEnter sales for the fourth quarter: ";
cin >> division.fourthQuarterSales;
validateSales(division.fourthQuarterSales);
}
/*******************************************************************************
* calculateSales
* Calculates the total annual sales and average quarterly sales for a division.
******************************************************************************/
void calculateSales(Division& division) {
division.totalAnnualSales = division.firstQuarterSales
+ division.secondQuarterSales + division.thirdQuarterSales
+ division.fourthQuarterSales;
division.averageQuarterlySales = division.totalAnnualSales / 4.0;
}
/*******************************************************************************
* displayDivisionData
* Displays the sales data for a division.
******************************************************************************/
void displayDivisionData(const Division& division) {
cout << "\nDivision: " << division.name << "\n";
cout << " First Quarter Sales: $" << division.firstQuarterSales << "\n";
cout << " Second Quarter Sales: $" << division.secondQuarterSales << "\n";
cout << " Third Quarter Sales: $" << division.thirdQuarterSales << "\n";
cout << " Fourth Quarter Sales: $" << division.fourthQuarterSales << "\n";
cout << " Total Annual Sales: $" << division.totalAnnualSales << "\n";
cout << " Average Quarterly Sales: $" << division.averageQuarterlySales
<< "\n";
}
/*******************************************************************************
* validateSales
* Ensures that the sales value is non-negative.
*******************************************************************************/
void validateSales(double& sales) {
while (sales < 0) {
cout << "Invalid sales figure. Sales cannot be negative. Please "
"re-enter: ";
cin >> sales;
}
}