C++ PROGRAM * Your input will come from a text file of at least 15 customers * I
ID: 3772104 • Letter: C
Question
C++ PROGRAM
* Your input will come from a text file of at least 15 customers
* Input file format - customerType accountNumber premiumChannels
( i.e residential example: R12345 5 , business example B12345 16 8 )
* Precision should be two decimal places
* 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.
Here is what i have so far.
#include <math.h>
#include <conio.h>
#include<iostream>
#include<fstream>
#include <string>
#include<stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
# include <Windows.h>
# include <mmsystem.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;
using namespace std;
void main()
{
//Variable Declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double amountDue;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "This program computes a cable bill." << endl;
cout << "Enter account number (an integer): ";
cin >> accountNumber;
cout << endl;
cout << "enter customer type: "
<< "R or r (Residential), "
<< "B or b (Business)";
cin >> customerType;
cout << endl;
if (customerType = 'r' || 'R')
{
cout << "Enter the number of premium channels:";
cin >> numOfPremChannels;
cout << endl;
amountDue = RES_BILL_PROC_FEES
+ RES_BASIC_SERV_COST
+ numOfPremChannels * RES_COST_PREM_CHANNEL;
cout << "Account Number: " << accountNumber;
cout << endl;
cout << "Amount Due: $" << amountDue << endl;
}
else if (customerType = 'b' || 'B')
cout << "Enter number of basic service connections: ";
cin >> numOfBasicServConn;
cout << endl;
cout << "Enter number of premium channels: ";
cin >> numOfPremChannels;
cout << endl;
if (numOfBasicServConn <= 10)
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
else
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10) *
BUS_BASIC_CONN_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
cout << "Account Number: " << accountNumber << endl;
cout << "Amount Due: $" << amountDue << endl;
cout << endl;
system("pause");
return ;
}
Explanation / Answer
/**C++ progrma that opens an input file contains residential and business count
cable bills and write the account number and dues , average spending of residential
and business to output file "bill.txt"
*/
#include <math.h>
#include <conio.h>
#include<iostream>
#include<fstream>
#include <string>
#include<stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
# include <Windows.h>
# include <mmsystem.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;
using namespace std;
void main()
{
//Variable Declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double amountDue;
double residentialTotal=0;
int residentialCount=0;
double businessTotal=0;
int businessCount=0;
cout << fixed << showpoint;
cout << setprecision(2);
//open input file
fstream fin;
fin.open("customers.txt");
//open output file
ofstream fout;
fout.open("bill.txt");
if(!fin)
{
cout<<"File does not exist."<<endl;
exit(0);
}
fin>>customerType;
while(!fin.eof())
{
if ((customerType == 'r') ||( customerType =='R'))
{
fin>>accountNumber>>numOfPremChannels;
amountDue = RES_BILL_PROC_FEES
+ RES_BASIC_SERV_COST
+ numOfPremChannels * RES_COST_PREM_CHANNEL;
//sum amount due for business
residentialTotal+=amountDue;
//increment the residential count
residentialCount++;
}
else if ((customerType == 'b') ||( customerType =='B'))
{
fin>>accountNumber>> numOfBasicServConn >> numOfPremChannels;
if (numOfBasicServConn <= 10)
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
else
amountDue = BUS_BILL_PROC_FEES
+ BUS_BASIC_SERV_COST
+ (numOfBasicServConn - 10) *
BUS_BASIC_CONN_COST
+ numOfPremChannels *
BUS_COST_PREM_CHANNEL;
//calculate business clients
businessCount++;
//sum amount due for business
businessTotal+=amountDue;
}
//write account number and amount due
fout<<"Account Number: " << accountNumber<<" Amount Due: $" << amountDue << endl;
//read customer type
fin>>customerType;
}
//calculate residential and business average spending
double residentialAvg=residentialTotal/residentialCount;
double businessAvg=businessTotal/businessCount;
fout<<"Residential average spending "<<residentialAvg<<endl;
fout<<"Business average spending "<<businessAvg<<endl;
fin.close();
fout.close();
system("pause");
return ;
}
-------------------------------------------------------------------------------------
R 12345 5
B 12345 16 8
R 12345 5
B 12345 16 8
B 12345 16 8
B 12345 16 8
B 12345 16 8
B 12345 16 8
B 12345 16 8
B 12345 16 8
B 12345 16 8
R 12345 5
R 12345 5
R 12345 5
R 12345 5
---------------------------------------------------------------------------
bill.txt output file
Account Number: 12345 Amount Due: $62.5
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $62.5
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $520
Account Number: 12345 Amount Due: $62.5
Account Number: 12345 Amount Due: $62.5
Account Number: 12345 Amount Due: $62.5
Account Number: 12345 Amount Due: $62.5
Residential average spending 62.5
Business average spending 520
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.