Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can I please get help with fixing this code C++ so It can work. These is the question and Input file Assignment: You are given

can I please get help with fixing this code C++ so It can work.

These is the question and Input file

Assignment:

You are given an input file rates.txt with the following sample data:

NewYork Delhi 1.99 2.00 0.45

NewYork London 1.50 1.75 0.35

Chicago Bombay 1.99 2.00 0.45

Chicago Bangkok 1.75 1.95 0.40

Each input line consists of the city from where the call is placed, the city to which the call is placed, the connection fee, amount for the first three minutes, and the amount for every additional minute after the first three minutes. Write a program that calculates the amount for a call placed for 15 minutes, 30 minutes, 45 minutes, and 1 hour (60 minutes), and stores it in an output file called a05output.txt. Format the output to two decimal places.

Requirements:

You must use the function prototypes given below. If input file or output file does not open, output an error message and exit the program. You can add more functions if you like.

bool openInputFile(ifstream &, string filename);

bool openOutputFile(ofstream &, string filename);

void calculate(ifstream &, ofstream &);

Below is my code.

#include

#include

#include

#include

using namespace std;

//Function prototypes

bool openInputFile(ifstream &, string filename);

bool openOutputFile(ofstream &, string filename);

void calculate(ifstream &, ofstream &);

double calculateMins(int minutes, double connectionFee, double firstMinsFee, double additionalMinsFee);

int main()

{

//Declaring Variables

string inputFilename = "city.txt";

string outputFilename = "a05output.txt";

ifstream inputFile;

ofstream outputFile;

//checking if the file opens successfully

if (!openInputFile(inputFile,inputFilename))

{

cout <<"unable to open input file " <

return 1;

}

if (!openOutputFile(outputFile, outputFilename))

{

cout <<"unable to open output file" << outputFilename << endl;

return 1;

}

calculate(inputFile, outputFile);

inputFile.close();

outputFile.close();

cout <<"Output file " << outputFilename << endl;

}

bool openIputFile(ifstream& inputFile, string filename)

{

inputFile.open(filename.c_str());

if (inputFile.is_open())

{

return true;

}

else

return false;

}

bool openOutputFile(ofstream &outputFile, string filename)

{

outputFile.open(filename.c_str());

if (outputFile.is_open())

{

return true;

}

else

return false;

}

void calculate(ifstream &inputFile, ofstream &outputFile)

{

string from,to;

double connectionFee, firstMinsFee, addtionalMinsFee;

outputFile << setprecision(2) << fixed;

outputFile << left << setw(15) << "City From" << setw(15) << "City To ";

outputFile << setw(10) << "15 mins" << setw(10) << "30 mins" << setw(10) << "45 mins" << setw(10) << "60 mins" << endl;

while (inputFile >> from)

{

inputFile >> to >> connectionFee >> firstMinsFee >> addtionalMinsFee;

outputFile << left << setw(15) << from << setw(15) << to;

outputFile << setw(10) << calculateMins(15, connectionFee, firstMinsFee, addtionalMinsFee);

outputFile << setw(10) << calculateMins(30, connectionFee, firstMinsFee, addtionalMinsFee);

outputFile << setw(10) << calculateMins(45, connectionFee, firstMinsFee, addtionalMinsFee);

outputFile << setw(10) << calculateMins (60, connectionFee, firstMinsFee, addtionalMinsFee);

outputFile << endl;

}

}

double calculateMins(int minutes, double connectionFee, double firstMinsFee, double additionalMinsFee)

{

double total = connectionFee + firstMinsFee;

double extraMins = minutes - 3;

if(extraMins > 0)

total += extraMins * additionalMinsFee;

return total;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions