C++ Calculate the running average for residential and business customer spending
ID: 3772135 • Letter: C
Question
C++
Calculate the running average for residential and business customer spending
Print all customer's bill to a single file and the end of the file you should have the average summary for each customer type.
My file is customers.txt for read, and calculations.txt for output file.
I need the running average code so that when I look in calculations.txt I see the averages of business and residential spending at the end.
Here is what I have so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
//Named constants – residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.50;
//Named constants – business customers
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;
int main()
{
//Variable declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double resAmountDue = 0;
double busAmountDue = 0;
int count = 0;
int customers;
double sum = 0;
int resCustomerTotal = 0;
int busCustomerTotal = 0;
ifstream inFile;
inFile.open("customers.txt");
ofstream outFile;
outFile.open("calcuations.txt");
cout << "Please enter the number of customers in the list: ";
cin >> customers;
cout << endl;
//outFile << sum;
for (count = 0; count < customers; count++)
{
((inFile >> customerType));
if (customerType == 'R' || customerType == 'r')
{
resCustomerTotal = resCustomerTotal + 1;
((inFile >> accountNumber >> numOfPremChannels));
resAmountDue = (numOfPremChannels * RES_COST_PREM_CHANNEL)
+ RES_BASIC_SERV_COST
+ RES_BILL_PROC_FEES;
outFile << " ";
outFile << " " << customerType << " " << accountNumber << " ";
outFile << numOfPremChannels << " $" << resAmountDue;
}
else if (customerType == 'B' || customerType == 'b')
{
((inFile >> accountNumber >> numOfBasicServConn >> numOfPremChannels));
busCustomerTotal = busCustomerTotal + 1;
if (numOfBasicServConn <= 10)
{
busAmountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
}
else
{
busAmountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10) *
BUS_BASIC_CONN_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
outFile << " ";
outFile << " " << customerType << " " << accountNumber << " "
<< numOfBasicServConn << " " << numOfPremChannels << " $" << busAmountDue;
}
}
else if (customerType != 'R' || customerType != 'r' || customerType != 'B' || customerType != 'b')
{
outFile << "Invalid customer type." << endl;
}
}
count++;
cout << " ";
}
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
//Named constants – residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.50;
//Named constants – business customers
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;
int main(){
//Variable declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double resAmountDue = 0;
double busAmountDue = 0;
int count = 0;
int customers;
double sum_1 = 0;
double sum_2 = 0;
int resCustomerTotal = 0;
int busCustomerTotal = 0;
ifstream inFile;
inFile.open("customers.txt");
ofstream outFile;
outFile.open("calcuations.txt");
cout << "Please enter the number of customers in the list: ";
cin >> customers;
cout << endl;
for (count = 0; count < customers; count++){
((inFile >> customerType));
if (customerType == 'R' || customerType == 'r'){
resCustomerTotal = resCustomerTotal + 1;
((inFile >> accountNumber >> numOfPremChannels));
resAmountDue = (numOfPremChannels * RES_COST_PREM_CHANNEL) + RES_BASIC_SERV_COST
+ RES_BILL_PROC_FEES;
outFile << " ";
outFile << " " << customerType << " " << accountNumber << " ";
outFile << numOfPremChannels << " $" << resAmountDue;
sum_1 += resAmountDue;
}
else if (customerType == 'B' || customerType == 'b'){
((inFile >> accountNumber >> numOfBasicServConn >> numOfPremChannels));
busCustomerTotal = busCustomerTotal + 1;
if (numOfBasicServConn <= 10){
busAmountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfPremChannels *
BUS_COST_PREM_CHANNEL);
}
else{
busAmountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10) * BUS_BASIC_CONN_COST + (numOfPremChannels *
BUS_COST_PREM_CHANNEL);
}
outFile << " ";
outFile << " " << customerType << " " << accountNumber << " "
<< numOfBasicServConn << " " << numOfPremChannels << " $" << busAmountDue;
sum_2 += busAmountDue;
}
else if (customerType != 'R' || customerType != 'r' || customerType != 'B' || customerType != 'b'){
outFile << "Invalid customer type." << endl;
}
}
outFile << "Average of residential spending " << sum_1/resCustomerTotal << endl;
outFile << "Average of business spending " << sum_2/busCustomerTotal << endl;
count++;
cout << " ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.