the function,and prliRE Ehe Base eust ai electricity for customers. The program
ID: 3915324 • Letter: T
Question
the function,and prliRE Ehe Base eust ai electricity for customers. The program should read the following information from an input file named "chargin.dat" customer account number (integer) kilowatt hours used (float) status (char) Electric charges are 59 cents per kilowatt hour for commercial customers and 47 cents per kilowatt hours for residential customers. The status will be "r" tor residential customers and "c" for commercial customers. You do not know how many lines of data are in the input file. The program should place the following information for each customer in an output file named "chargout.dat" customer account number cost of electricity (float) The output file will be used as input to another program SAMPLE INPUT FILE: 143 47.3 r 782 245.7 c SAMPLE OUT PUT FILE: 143 22.231 782 144.963Explanation / Answer
/**
The c++ program that read the data from the file chargin.dat
and then find the charges for commercial and residential houses
and then write the result to the output file,chargout.dat file.
*/
//A
//include header files
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
//start of main function
int main()
{
//rate of kilowatt per hour
const float COMMERCIAL=0.59;
const float RESIDENTIAL=0.47;
string file="chargin.dat";
//Open file object to read input from file chargein.dat
ifstream inFile;
inFile.open(file);
//declare variables to store data from file,chargin.dat
int cusNumber;
float kwPerHour;
char status;
//check if file not exist
if(!inFile)
{
cout<<"File Do Not Exist."<<endl;
system("pause");
return 0;
}
else
{
//Open output file stream object
ofstream outFile;
outFile.open("chargout.dat");
//read records from the file until no record is left
while(inFile>>cusNumber>>kwPerHour>>status)
{
//write the customer number and charage to output file
if(status=='r')
outFile<<cusNumber<<" "<<kwPerHour*RESIDENTIAL<<endl;
else if(status=='c')
outFile<<cusNumber<<" "<<kwPerHour*COMMERCIAL<<endl;
}
//close the input and output file stream
inFile.close();
outFile.close();
}//end of else
//pause program output on console
system("pause");
return 0;
}
Sample Input: chargin.dat
143 47.3 r
782 245.7 c
sample output file : chargout.dat
143 22.231
782 144.963
---------------------------------------------------------------------------------------------------------------------------------
//B
/**
The c++ program that read the data from the file chargout.dat
and then print the data to console with neatly formatted.
*/
//B
//include header files
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
//start of main function
int main()
{
string file="chargout.dat";
//Open file object to read input from file chargeout.dat
ifstream inFile;
inFile.open(file);
//declare variables to store data from file,chargout.dat
int cusNumber;
double due;
//check if file not exist
if(!inFile)
{
cout<<"File Do Not Exist."<<endl;
system("pause");
return 0;
}
else
{
cout<<"Electric Charges"<<endl<<endl;
//read records from the file until no record is left
while(inFile>>cusNumber>>due)
{
cout<<left<<setw(15)<<"Customer: "<<cusNumber<<endl;
cout<<setw(15)<<"Amount Due:"<<"$"<<due<<endl;
cout<<endl;
}
//close the input and output file stream
inFile.close();
}//end of else
//pause program output on console
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------
Input file to read : chargout.dat
143 22.231
782 144.963
--------------------------------------------------------------------------------------------------------
Sample Output:
Electric Charges
Customer: 143
Amount Due: $22.231
Customer: 782
Amount Due: $144.963
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.