Using only #include<iostream> please. Thank you very much! Instructions you have
ID: 3849725 • Letter: U
Question
Using only #include<iostream> please. Thank you very much!
Instructions you have been tasked with writing a program to generate a quarterly sales report for your company. The data is in the "sales. txt" file (Under the Materials category in this week's module on Canvas). This file contains records similar to: 2013-1003 B 120.00 The first field is the invoice number which is not used, the second is an equipment code and the third is the cost of the parts on that order. The equipment code will be: A Capital Equipment B Expensed Equipmen C Small Parts your program should read a line from the file into a struct, pass the struct and three accumulator variables to a function and then continue reading lines and calling the accumulator function until it reaches end of file. It should then call the writeReport function to write the report into a text file, inventoryReport.txt: S A L E S R E P O R T Capital Equipment $24093.18 26.31% $22222.80 24.27% Expensed Equipment $45251.98 49.42% Small Parts $91567.96 Total Sales The prototype of the accumulator function will be similar to: void accumulate (const SalesRecord. &s; double & Csales, double &E; sales, double & sales)Explanation / Answer
Updated code below to just use struct without typedef. Hope it helps.
Also to answer the other questions....
1) We pass the struct as a reference variable since we don't want to have another copy of it in the function and reuse the same.
2) By using the const for hte struct variable, we ensure that we don't modify its contents accidently in the function. const variables are not modifiable
3) Using both makes sense since we now are just using the same copy of variable from main and ensuring that we don't modify its contents by using const keyword.
===========
Here is the code for the question. Output is shown below. Also <fstream> should be included if any files I/O needs to be done. Since you did not mention whether #include <iomanip> could be used, I could not use setprecision() for decimal places or setw() to format the output . I have just to format the output.
Please don't forget to rate the answer if it helped. Thank you very much.
#include <iostream>
#include <fstream>
using namespace std;
struct SalesRecord{
string invoice_no;
char equipment_type;
double cost;
};
void accumulate(const SalesRecord &s, double &Csales, double &Esales, double &Ssales);
int main()
{
string filename = "sales.txt";
ifstream infile;
//cout << "Enter input filename: ";
//cin >> filename;
infile.open(filename);
if(infile.fail())
{
cout << "Input file " << filename << " not found! " << endl;
exit(1);
}
SalesRecord record; //variable to store a record from file
//variables to store total cost for capital , expensed and small parts
double CSales = 0, ESales = 0, SSales = 0;
while( infile >> record.invoice_no ) // while there is another record
{
infile >> record.equipment_type >> record.cost; //read the remaining fields
accumulate(record, CSales, ESales, SSales);
}
infile.close();
double total = CSales + ESales + SSales; //calculate total cost
double percent;
//open an output file
ofstream outfile("inventoryReport.txt");
outfile << " S A L E S R E P O R T" << endl << endl;
//calculate percentage for capital equipments
percent = CSales * 100 / total;
outfile << "Capital Equipment $" << CSales << " " << percent << "%" << endl;
//calculate percentage for expensed equipments
percent = ESales* 100 / total;
outfile << "Expensed Equipment $" << ESales << " " << percent << "%" << endl;
//calculate percentage for small parts
percent = SSales * 100 / total;
outfile << "Small Parts $" << SSales << " " << percent << "%" << endl;
outfile << " ------------" << endl;
outfile << "Total Sales $" <<total << endl;
outfile.close();
cout << "output file inventoryReport.txt generated." << endl;
}
void accumulate(const SalesRecord &rec, double &Csales, double &Esales, double &Ssales)
{
if(rec.equipment_type == 'A')
Csales += rec.cost;
else if(rec.equipment_type == 'B')
Esales += rec.cost;
else if(rec.equipment_type == 'C')
Ssales += rec.cost;
}
sample input file sales.txt
2013-1003 B 120.45
2014-1004 C 100.80
2014-1101 A 150.25
2013-1003 B 220.05
2014-1004 C 200.00
2014-1101 A 550.50
output file inventoryReport.txt
S A L E S R E P O R T
Capital Equipment $700.75 52.2149%
Expensed Equipment $340.5 25.3716%
Small Parts $300.8 22.4135%
------------
Total Sales $1342.05
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.