C++ HELP!!! Write 2 programs: One program will use a structure to store the foll
ID: 3688788 • Letter: C
Question
C++ HELP!!!
Write 2 programs:
One program will use a structure to store the following data on a company division:
Division Name (such as East, West, North, or South)
Quarter (1, 2, 3, or 4)
Quarterly Sales
The user should be asked for the four quarters' sales figures for the East, West, North, and South divisions. The data for each quarter for each division should be written to a file.
The second program will read the data written by the first program. The program should calculate and display the following figures:
Total corporate sales for each quarter
Total yearly sales for each division
Total yearly corporate sales
Average quarterly sales for the divisions
The highest and lowest quarters for the corporation
Input Validation: Do not accept negative numbers for any sales figures.
Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int LENGTH = 4;
struct CorpData
{
static string Division[LENGTH];
double Qtr[LENGTH];
double QtrlySales;
};
string CorpData::Division[LENGTH] = {"East", "West", "North", "South"};
int main()
{
CorpData Sales;
double TotCorpQtr[LENGTH] = {0,0,0,0}; // Total corporate sales for each quarter
double TotYrDiv, // Total yearly sales for each division
TotYrCorp, // Total yearly corporate sales
Highest,
Lowest;
TotYrCorp = TotYrDiv = 0;
fstream File("salefigures.txt", ios::in | ios::binary);
if (!File)
{
cout << "Error opening file. Program aborting. ";
return 0;
}
// Display sales figures.
cout << fixed << showpoint << setprecision(2);
cout << " Corporate Sales Data Report "
<< "--------------------------- ";
cout << " Total sales by division: ";
for (int d = 0; d < LENGTH; d++)
{
File.read(reinterpret_cast<char *>(&Sales), sizeof(Sales));
cout << Sales.Division[d] << ": ";
cout << " Total yearly Sales: $";
for (int q = 0; q < LENGTH; q++)
{
TotYrDiv += Sales.Qtr[q];
TotCorpQtr[q] += Sales.Qtr[q];
}
cout << TotYrDiv << endl;
cout << " Average quarterly sales: $" << TotYrDiv/4 << endl;
TotYrCorp += TotYrDiv;
}
cout << " Total corporate sales for each quarter: ";
for (int i = 0; i < LENGTH; i++)
{
cout << "Quarter " << (i + 1) << ": $" << TotCorpQtr[i] << endl;
}
cout << " Total yearly corporate sales: $" << TotYrCorp << endl;
Highest = Lowest = TotCorpQtr[0];
for (int i = 1; i < LENGTH; i++)
{
if (TotCorpQtr[i] > Highest)
Highest = TotCorpQtr[i];
if (TotCorpQtr[i] < Lowest)
Lowest = TotCorpQtr[i];
}
cout << "The highest quarter for the corporation: " << Highest << endl;
cout << "The lowest quarter for the corporation: " << Lowest << endl;
File.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.