Rewrite the Programming Example page 236 in chapter 4 ( Cable Company Billing ).
ID: 3854644 • Letter: R
Question
Rewrite the Programming Example page 236 in chapter 4 ( Cable Company Billing ).
Expected Program and Design:
1. Write a pseudo code before starting your program ( do not use SWITCH, replace it with IF structures )
1.1 Draw a flowchart for your program based on your pseudo code
2. Identify your constants
3. Your input will come from a text file of at least 15 customers
3.1 Input file format - customerType accountNumber premiumChannels
( i.e residential example: R12345 5 , business example B12345 16 8 )
4. Precision should be two decimal places
5. Calculate the running average for residential and business customer spending
6. 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.
6.1 Pay attention to details when you formatting your output
Note: Use all chapter concepts and make your final as a true representation of what we have learned this semester.
Turn in
program design ( pseudo code and/or flow chart )
input file
output file
.cpp file of your program ( make sure you include your header and comment your code )
Extra credit
Implement your program using arrays, covered in chapter 8, and user defined simple data types to hold customer data.
#include <iostream>
#include <fstream>
#include <iomanip>
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 amountDue;
int rescount=0,buscount=0;
double ressum=0.00,bussum=0.00;
double resavg,busavg;
ifstream fin;
ofstream fout;
//open input file
fin.open("custin.txt",ios::in);
if(fin.fail())
{
cout<<"Unable to open file "<<endl;
exit(0);
}
//open ouput file
fout.open("custout.txt",ios::out);
fout << fixed << showpoint;
fout << setprecision(2);
fout << "This program computes a cable "
<< "bill." << endl;
//read from input file
while(!fin.eof())
{
//read customer type from file
fin >> customerType;
//read account number from file
fin >> accountNumber;
//If customer type is residential
if(customerType=='r' || customerType=='R' )
{
//read number of prem channels from file
fin >> numOfPremChannels;
amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels * RES_COST_PREM_CHANNEL;
//write into output file
fout << "Account number: " << accountNumber << endl;
fout << "Amount due: $" << amountDue << endl<<endl;
//compute running average of residential cusomer's amount due
rescount+=1;
ressum+=amountDue;
resavg=ressum/rescount;
}
//If customer type is business
else if(customerType=='b' || customerType=='B' )
{
//read number of service connection from file
fin >> numOfBasicSevConn;
//read number of prem channels from file
fin >> 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;
fout << "Account number: " << accountNumber << endl; //Step 7f
fout << "Amount due: $" << amountDue << endl<<endl; //Step 7f
//compute running average of business cusomer's amount due
buscount+=1;
bussum+=amountDue;
busavg=bussum/buscount;
}
else
fout << "Invalid customer type.in "<<accountNumber << endl; //Step 8
}
fout<<endl<<"Average Due for residential Customer "<<resavg<<endl;
fout<<endl<<"Average Due for Business Customer "<<busavg<<endl;
fin.close();
fout.close();
return 0;
}
How would I implement arrays? And is this correct?
Does this code effeciently use
Input/output
Control Structures
User Defined Functions
User Defined simple data type/namepsaces/strings
I feel like it does seem to work, but I'm stuck on how to implement this into an array
Explanation / Answer
To implement this using arrays and user defined data types, you could declare a struct with all the information needed to store. You didn't give me the original question, so I can't really write the code for you myself. But I can surely help you in writing the code.
For example:
If you have to take an input of the customer type and account number from the file.
So, you declare a struct like:
struct cust
{
char type;
int num;
};
Then, declare an array of structs like
cust c[10];
Then, as you iterate through each loop, use a counter variable to store the values of each customer in the array
Example:
while(!fin.eof())
{
fin>>c[i].type;
fin>>c[i].num;
i++;
}
As for your questions. The code does implement input output and control stuctures efficiently.
There are however, no user defined functions. You can create one in the following manner.
Suppose you want to make a function which stores the type and number into the array of structs.
Declare the function like:
void store(char t,int n)
{
c[i].type=t;
c[i].num=n;
i++;
}
P.S. Make sure you declare i as a global variable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.