Write a program that uses a structure to store the following data on a company d
ID: 3912859 • Letter: W
Question
Write a program that uses a structure to store the following data on a company division.
- Division Name (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 called salesreport.txt.
Input validation
- Do not accept negative numbers for any sales figures.
- Standardize user’s Division Name input to East, West, North or South
Please also answer the following questions:
1. What members will your structure contain? What is the data type of each member?
2. How will data be organized in your output file.
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
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;
fstream File("salefigures.txt", ios::out | ios::binary);
if (!File)
{
cout << "Error opening file. Program aborting. ";
return 0;
}
// Ask user for the four quarters' sales figures
// for the East, West, North, and South divisions.
cout << "Enter the quarterly sales figures for each of the divisions: ";
for (int d = 0; d < LENGTH; d++)
{
cout << Sales.Division[d] << "Division: ";
for (int q = 0; q < LENGTH; q++)
{
do
{
cout << "Quarter " << (q + 1) << ": ";
cin >> Sales.QtrlySales;
if (Sales.QtrlySales < 0)
cout << "Sales figures must be greater than 0. ";
} while (Sales.QtrlySales < 0);
Sales.Qtr[q] = Sales.QtrlySales;
}
File.write(reinterpret_cast<char *>(&Sales), sizeof(Sales));
}
File.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.