CIS210 Introduction to C Programming Fall 2016 AssignmentX P
Solution
electric_info.txt(Save this file under D Drive.Then the path of the file pointing to the file is D:electric_info.txt)
Tom 123-49-82 2100
Mark 241-87-11 346
Nancy 198-85-07 5002
Beth 342-53-49 2812
Erin 787-63-88 688
________________________
C++ code
#include <iostream>
#include <iomanip>
#include <fstream>
#define FEDERAL_AND_LOCAL_TAX 0.10
using namespace std;
int main()
{
//Declaring variables
string name,account_num;
int kilowatt_hours;
double bill_amount=0.0,tot_bill_incl_tax=0.0;
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Opening the input file
dataIn.open(\"D:\\\\electric_info.txt\");
//creating and Opening the output file
dataOut.open(\"D:\\\\bill_info.txt\");
//Reading single line from input file every time
while(dataIn>>name>>account_num>>kilowatt_hours)
{
/* Based on the range of the no of kilowatt hours
* bill amount and total bill
* amount will be calculated
*/
if(kilowatt_hours>=0 && kilowatt_hours<=400)
{
//Calculating the bill amount bsed on number of Kilowatt hours
bill_amount=kilowatt_hours*0.08;
}
else if(kilowatt_hours>=401 && kilowatt_hours<=1500)
{
//Calculating the bill amount bsed on number of Kilowatt hours
bill_amount=kilowatt_hours*0.15;
}
else if(kilowatt_hours>=1501 && kilowatt_hours<=3500)
{
//Calculating the bill amount bsed on number of Kilowatt hours
bill_amount=kilowatt_hours*0.24;
}
else if(kilowatt_hours>=3501 )
{
//Calculating the bill amount bsed on number of Kilowatt hours
bill_amount=kilowatt_hours*0.29;
}
//Calculating the tottal bill amount including federal and local tax
tot_bill_incl_tax=bill_amount+bill_amount*FEDERAL_AND_LOCAL_TAX;
//Writing the customer name to the output file
dataOut<<\"Customer Name: \"<<name<<endl;
//Writing the Account number to the output file
dataOut<<\"Account Number: \"<<account_num<<endl;
//Writing the Total bill amount to the output file
dataOut<<\"BillAmount: \"<<std::fixed<< std::setprecision(2)<<tot_bill_incl_tax<<\"\ \"<<endl;
}
return 0;
}
_______________________________
bill_info.txt (After running the program we can see this file in D:Drive as we specified the path of the output file is D:\\\\bill_info.txt )
Customer Name: Tom
Account Number: 123-49-82
BillAmount: 554.40
Customer Name: Mark
Account Number: 241-87-11
BillAmount: 30.45
Customer Name: Nancy
Account Number: 198-85-07
BillAmount: 1595.64
Customer Name: Beth
Account Number: 342-53-49
BillAmount: 742.37
Customer Name: Erin
Account Number: 787-63-88
BillAmount: 113.52
___________Thank You

