Format of the input data file(input filename is \"call data.txt\"): Do not inclu
ID: 3850645 • Letter: F
Question
Format of the input data file(input filename is "call data.txt"): Do not include column titles (The order of the columns are as follows: cell phone number, relays, minutes) 9546321555 50 5612971340 3051234567 25 7542346622 3054432762 15 100 50 9544321011 82 8776219988 87 9042224556 7877176590 45 5617278899 20 9546321555 5612971340 25 3051234567 118 7542346622 3054432762 115 9544321011 265 8776219988 9042224556 7877176590 5617278899 Format of output (output filename is "weekly call info.txt") (the order of the columns is as follows: cell phone number, relays, minutes, net cost, tax rate, call tax, total call cost) 001 9546321555Explanation / Answer
#include<iostream>
#include<fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
using namespace std;
//Class CallReport Definition
class CallReport
{
//Data member
string number;
int relay;
int callLength;
int numberOfRecords;
double netCost;
double taxRate;
double callTax;
double totalCost;
public:
//Member functions
//To read data from file
void Input(CallReport []);
//To calculate tax
void Process(CallReport []);
//To write data to file
void Output(CallReport []);
};//End of class
//Function to calculate tax
void CallReport::Process(CallReport rep[])
{
//Loops till end of records
for(int c = 0; c < numberOfRecords; c++)
{
//Checks the relay and fix the tax rate
if(rep[c].relay >= 0 && rep[c].relay <= 5)
rep[c].taxRate = 1;
else if(rep[c].relay >= 6 && rep[c].relay <= 11)
rep[c].taxRate = 3;
else if(rep[c].relay >= 12 && rep[c].relay <= 20)
rep[c].taxRate = 5;
else if(rep[c].relay >= 21 && rep[c].relay <= 50)
rep[c].taxRate = 8;
else
rep[c].taxRate = 12;
//Calculates net cost
rep[c].netCost = ( rep[c].relay / 50.0 * 0.40 * rep[c].callLength);
//Calculates call tax
rep[c].callTax = (rep[c].netCost * rep[c].taxRate) / 100;
//Calculates total cost
rep[c].totalCost = rep[c].netCost + rep[c].callTax;
}//End of for loop
}//End of function
//Function to write object contents into the file
void CallReport::Output(CallReport rep[])
{
//ofstream object created to write
ofstream writeFile;
//Opens the file weekly_call_info.txt for writing
writeFile.open("weekly_call_info.txt");
//Checks whether the file can be opened or not
if (writeFile.is_open())
{
//Write the headings
writeFile<<" Records in the file: ";
writeFile<<" Number Relays Minutes NetCost TaxRate Call Tax Total Call Cost";
//Loops till end of records
for(int c = 0; c < numberOfRecords; c++)
{
//Write the result to the file
writeFile<<" "<<rep[c].number<<" "<<rep[c].relay<<" "<<rep[c].callLength<<" ";
writeFile<<setprecision(2)<<fixed<<rep[c].netCost<<" "<<rep[c].taxRate<<" "<<rep[c].callTax<<" "<<rep[c].totalCost;
}//End of for loop
}//End of if
//If file cannot be opened
else
//Display the error message
cout<<" Error: Unable to open file";
//Close the file
writeFile.close();
//Display the result on console
cout<<" Records in the file: ";
cout<<" Number Relays Minutes NetCost TaxRate Call Tax Total Call Cost";
for(int c = 0; c < numberOfRecords; c++)
{
cout<<" "<<rep[c].number<<" "<<rep[c].relay<<" "<<rep[c].callLength;
cout<<" "<<setprecision(2)<<fixed<<rep[c].netCost<<" "<<rep[c].taxRate<<" "<<rep[c].callTax<<" "<<rep[c].totalCost;
}//End of for loop
}//End of function
//Function to read data from file
void CallReport::Input(CallReport rep[])
{
//Creates an object of ifstream
ifstream readf;
//Opens the file call_data.txt for reading
readf.open ("call_data.txt");
//Initializes number of records to zero
numberOfRecords = 0;
//To store data
char data[100];
//Loops till end of file
while(!readf.eof())
{
//Read data from file till tab space
readf.getline(data, 100, ' ');
//Stores the data in variable of class object
rep[numberOfRecords].number = data;
//Read data from file till tab space
readf.getline(data, 100, ' ');
//Converts the data to integer and store it in the variable
rep[numberOfRecords].relay = atoi(data);
//Read data from file till tab space
readf.getline(data, 100);
//Converts the data to integer and store it in the variable
rep[numberOfRecords].callLength = atoi(data);
//Increase the record counter
numberOfRecords++;
}//End of while
//Close file
readf.close();
}//End of function
//Main function
int main()
{
//Creates 100 objects of CallReport type
CallReport rec[100];
//Reads the file contents and stores in rec array of objects
rec[0].Input(rec);
//Calculates tax by calling the process() function
rec[0].Process(rec);
//Writes the result in file
rec[0].Output(rec);
return 0;
}//End of main
Sample Run:
Records in the file:
Number Relays Minutes NetCost TaxRate Call Tax Total Call Cost
9546321555 0 0 0.00 1.00 0.00 0.00
5612971340 5 50 2.00 1.00 0.02 2.02
6051234567 8 25 1.60 3.00 0.05 1.65
7542346622 24 17 3.26 8.00 0.26 3.53
3054432762 15 30 3.60 5.00 0.18 3.78
9544321011 50 100 40.00 8.00 3.20 43.20
8776219988 87 82 57.07 12.00 6.85 63.92
9042224556 4 5 0.16 1.00 0.00 0.16
7877176590 11 1 0.09 3.00 0.00 0.09
5617278899 20 45 7.20 5.00 0.36 7.56
9546321555 4 3 0.10 1.00 0.00 0.10
5612971340 79 86 54.35 12.00 6.52 60.87
3051234567 8 25 1.60 3.00 0.05 1.65
7542346622 24 118 22.66 8.00 1.81 24.47
3054432762 115 25 23.00 12.00 2.76 25.76
9544321011 43 10 3.44 8.00 0.28 3.72
8776219988 265 22 46.64 12.00 5.60 52.24
9042224556 2 5 0.08 1.00 0.00 0.08
7877176590 89 67 47.70 12.00 5.72 53.43
5617278899 40 56 17.92 8.00 1.43 19.35
Contents of input file (call_data.txt):
9546321555 0 0
5612971340 5 50
6051234567 8 25
7542346622 24 17
3054432762 15 30
9544321011 50 100
8776219988 87 82
9042224556 4 5
7877176590 11 1
5617278899 20 45
9546321555 4 3
5612971340 79 86
3051234567 8 25
7542346622 24 118
3054432762 115 25
9544321011 43 10
8776219988 265 22
9042224556 2 5
7877176590 89 67
5617278899 40 56