Create a SalesDataWrite class that receive info of sales from user and write the
ID: 3633983 • Letter: C
Question
Create a SalesDataWrite class that receive info of sales from user and write the data to a text file using structure. Info of sales includes:Division name (East, West, North, or South)
Quarter (1, 2, 3, or 4)
Quarterly sales
You must code the following member functions in SalesDataWrite class:
? A constructor accepts a string as file name that will be used to store the data.
? A member function called dataInput() that will ask user to input the info of sales and store the data into a structure.
? A member function called writeData() that will output the structure to the specified file.
In the driver code main() to test your class, you will ask user to enter the file name first, and then create an object of SalesDataWrite with the specified file name. Inside a while loop, you will ask user to enter the info of sales and you must verify the sales data cannot be negative. You will call other member functions to store the data and write out the data to the file. This process will continue until user enters ‘n’ in ether upper case or lower case to stop. You need to verify if user has entered ‘y’ or ‘n’ in either upper case or lower case to terminate the execution.
Your program must have the following files:
1. SalesDataWrite.h
2. SalesDataWrite.cpp
3. main.cpp
PARTII:
Create a SalesDataRead class that will read the file you have created from Part I and display the info of sales and the statistic report to the screen. The report includes:
Total sales for each quarter from all divisions
Total yearly sales for each division
The average quarterly sales for the divisions
Total overall corporate sales
The highest and lowest quarters for the corporation
You will decide the constructor(s) and member functions in SalesDataRead to accomplish the tasks described above. You must display the report with properly defined format.
In the driver code main() to test your class, you will ask user to enter the file name for reading and then create an object of SalesDataRead. You will call proper member functions you created to read the file and display the content of the file and the user-friendly statistic report.
Your program must have the following files:
1. SalesDataRead.h
2. SalesDataRead.cpp
3. main.cpp
Explanation / Answer
Dear,
//main.cpp
#include<iostream>
#include<fstream>
#include "SalesDataWrite.h"
using namespace std;
int main()
{
fstream fout;
char fileName[20];
cout<<"Enter Sales output fileName: ";
cin>>fileName;
SalesDataWrite salesObject(fileName);
salesObject.dataInput(salesObject,fout);
salesObject.writeData(salesObject,fout);
system("pause");
return 0;
}
//SalesDataWrite.h
#ifndef SALESDATAWRITE_H
#define SALESDATAWRITE_H
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class SalesDataWrite
{
//Variable declaration
string DivisionName;
int Q[4];
float qs[4];
public:
SalesDataWrite(string fName);
void dataInput(SalesDataWrite,fstream &);
void writeData(SalesDataWrite,fstream &);
};
#endif SALESDATAWRITE_H
//SalesDataWrite.cpp
#include<iostream>
#include<string>
#include<fstream>
#include "SalesDataWrite.h"
using namespace std;
SalesDataWrite::SalesDataWrite(string fileName)
{
fstream fout;
fout.open(fileName,ios::out);
}
void SalesDataWrite::dataInput(SalesDataWrite salesObject,fstream &fout)
{
for(int i=0;i<4;i++)
{
//Now writing the data to file
cout<<"Enter Division name "<<endl;
cin>>salesObject.DivisionName;
//Each Division has four
//Quarter and Each has its own Sales data
for(int j=0;j<4;j++)
{
//Do not accept negative values for
//Quarter and sales data
do{
cout<<"Enter Quarter Number"<<endl;
cin>>salesObject.Q[i];
cout<<"Enter Quarter sales "<<endl;
cin>>salesObject.qs[i];
}while(salesObject.qs[i]<0);
}
//Write the record data to the
//File associated with fout object
//
writeData(salesObject,fout);
}
}
void SalesDataWrite::writeData(SalesDataWrite sales,fstream &fout)
{
fout.write(reinterpret_cast<char*>(&sales),sizeof(sales));
//fout<<sales.DivisionName<<sales.Q<<sales.qs;
}
Note:fout.write() statement writes the output file in binary file
Please keep note.if we want to read ,we should use read() for binary files
One question for one post ..
Post part -2 in another post
Hope this would helpful to you