Answer to Question #261178 in C++ for dana

Question #261178

Write a C++ program that reads the number of sales (as integer values) at the end of each month for the ABC Supermarket of 5 departments (grocery, bakery, meat, vegetables, and electronics). The program reads all data from the input file sales.txt. The first line has an integer value N that represents the number of months to be processed. The following N input lines contain the name of the month followed by the sales of each department in that month (5 integer values).

Your program shall produce a nice report in a file report.txt showing the list of months and the sum of 5 sales for that month. The program will also output the total sum of all sales and the average sales per month. Format your output to produce an exact result as in the sample below. To output the # use setw(3), to output the month name use setw(6) and use setw(12) to output the sum of sales for the month. The final output should be limited to 2 decimal places.


1
Expert's answer
2021-11-04T11:43:14-0400
file sales.txt:


7
SEP 3 5 2 4 1
OCT 5 1 8 9 2
NOV 8 2 4 10 3
DEC 6 5 1 7 4
JAN 4 1 9 8 2
FEB 1 4 2 11 36
MAR 3 1 9 15 24


#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>


using namespace std;




int main()  {
	string fileName="sales.txt";


	ifstream salesFstream(fileName);
	if(!salesFstream){
		cout << "The file '"<<fileName<<"' does not exist.\n";
	}else{
		int N;
		int totalSales=0;
		salesFstream>>N;		
		ofstream reportFile;
		reportFile.open("report.txt");
		reportFile <<"Total sales per month:\n\n";
		reportFile <<setw(3)<<"#"<<setw(8)<<"Month"<<setw(13)<<"Sales\n";
		reportFile <<"===========================\n"; 
		for(int i=0;i<N;i++){
			int sum=0;
			string monthName;
			salesFstream>>monthName;
			int sale;
			for(int j=0;j<5;j++){	
				salesFstream>>sale;
				sum+=sale;
				totalSales+=sale;
			}
			reportFile <<setw(3)<<(i+1)<<setw(6)<<monthName<<setw(12)<<sum<<"\n";
		}
		float averageSales=totalSales/(float)N;
		reportFile <<"\nTotal sales in "<<N<<" Months is: "<<totalSales<<"\n";
		reportFile <<"Average sales per month is: "<<fixed<<setprecision(2)<<averageSales<<"\n";
		reportFile.close();
		cin>>N;
	}
	salesFstream.close();
	return 0;  
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS