Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING C++ CODING Objectives: 1. Read the contents of a datafilel one record at a

ID: 3870131 • Letter: U

Question

USING C++ CODING

Objectives: 1. Read the contents of a datafilel one record at a time; 2. Process the data that was read from the datafile one record at a time; 3. Print a record to a datafile using an ofstream object; 4. Be able to use the fstream library; 5. Be able to use records (class with only data); 6. Be able to use an ifstream object; 7. Be able to use an ofstream object; This assignment is an extension of Assignment#2. You will implement a program called "call stats3.cpp" to process customer call records. Each customer call record contains seven fields, which are as follows: 1) a ten digit cell phone number (string, no dashes), 2) the number of relay stations used in making the call (integer), 3) the length of the call in minutes (integer), 4) the net cost of the call (double), 5) the tax rate (double), 6) the call tax (double) and 7) the total cost of the call (double). Your program will have 3 functions: Input, Process and Output. Your main program will call each function until the end of the datafile has been read. Each call record will be printed to a data file. Following are the descriptions of the functionality of each function 1. The void function "Input" will have two parameters: an input file stream called "in", and a customer call record called "call records". The function will read the cell number, relays, and call_length, in minutes, into the a call record from the data file. 2. The function "Process" will calculate the net cost of a call (net cost), the tax on a call (call tax) and the total cost of the call (total cost) using the number of relay stations (relays) and the length in minutes of the call (call length) for a call record. Please consider the following: a. The tax rate on a call (call tax) is simply based on the number of relay stations (relays) used to make the call (0

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>

using namespace std;

ifstream fin("call_data.txt");
ofstream fout("weekly_call_info.txt");

long int phno;
int relay;
int mins;

float net_cost ,tax_rate, call_tax, total_call_cost;

void Process();
void Display();

void Input()
{
   char line[100];

   while(fin>>line)
    {
     if(fin.eof()) break;
     phno=atol(line);
     fin >> line;
     relay=atoi(line);
     fin >> line;
     mins = atoi(line);

     Process();
    }
   fin.close();
}

void Process()
{
net_cost = relay / 50 * 0.4 * mins;

if ( relay>=0 && relay<=5 )
tax_rate= 0.01;
else if ( relay>=6 && relay<=11 )
tax_rate= 0.03;
else if ( relay>=12 && relay<=20 )
tax_rate= 0.05;
else if ( relay>=21 && relay<=50 )
tax_rate= 0.08;
else if ( relay>=50 )
tax_rate=0.12;

call_tax = net_cost * tax_rate;
total_call_cost = net_cost + call_tax;
Display();
}

void Display()
{
char result[1000];

sprintf(result,"%-20ld %10d %10d %10.2f %10.2f %10.2f %10.2f", phno, relay, mins, net_cost,tax_rate,call_tax,total_call_cost);

fout << result << endl;
}

int main()
{
Input();
return 0;
}

/*

lenovo@lenovo-Vbox:~/chegg$ g++ -Wall call_stats3.cpp -o call_stats3
lenovo@lenovo-Vbox:~/chegg$ cat call_data.txt
9223351454            0               0
8734123434            5              50
2323232321            8              25
8765454231           24              17
9834343234           15              30
3232223893           50             100
6787868768           87              82
8997651211            4               5
2309883490           11               1
9223351454           20              45
8734123434            4               3
2323232321           79              86
8765454231            8              25
9834343234           24             118
3232223893          115              25
6787868768           43              10
8997651211            2               5
2309883490           89              67

lenovo@lenovo-Vbox:~/chegg$ ./call_stats3
lenovo@lenovo-Vbox:~/chegg$ ls
1to100.c   call_data.txt   call_stats3.cpp   cmplx       err.txt lab5.c       readseq.cpp   simu.c~
1to100.c~ call_data.txt~ call_stats3.cpp~ cmplx.cpp   fib.c    lab5.c~      readseq.cpp~ weekly_call_info.txt
a.out      call_stats3     ccmplx            cmplx.cpp~ fib.c~   numbers.txt simu.c
lenovo@lenovo-Vbox:~/chegg$ cat weekly_call_info.txt
9223351454                    0          0       0.00       0.01       0.00       0.00
8734123434                    5         50       0.00       0.01       0.00       0.00
2323232321                    8         25       0.00       0.03       0.00       0.00
8765454231                   24         17       0.00       0.08       0.00       0.00
9834343234                   15         30       0.00       0.05       0.00       0.00
3232223893                   50        100      40.00       0.08       3.20      43.20
6787868768                   87         82      32.80       0.12       3.94      36.74
8997651211                    4          5       0.00       0.01       0.00       0.00
2309883490                   11          1       0.00       0.03       0.00       0.00
9223351454                   20         45       0.00       0.05       0.00       0.00
8734123434                    4          3       0.00       0.01       0.00       0.00
2323232321                   79         86      34.40       0.12       4.13      38.53
8765454231                    8         25       0.00       0.03       0.00       0.00
9834343234                   24        118       0.00       0.08       0.00       0.00
3232223893                  115         25      20.00       0.12       2.40      22.40
6787868768                   43         10       0.00       0.08       0.00       0.00
8997651211                    2          5       0.00       0.01       0.00       0.00
2309883490                   89         67      26.80       0.12       3.22      30.02
lenovo@lenovo-Vbox:~/chegg$

*/