Answered! Task Create a C++ application which wi read a file of daily payments, calculate the total as well as the average…

Task Create a C++ application which wi read a file of daily payments, calculate the total as well as the average payment, display the results to the screen and write the results to a f e. The input and output file names should be provided as command line arguments. task 3.cpp The task 3.cpp file holds the main() function which is responsible for the following functionality: Extracts the input file name and output file name from the command line arguments. o lfthe number of command line arguments is not correct, throw an exception. The exception class is Argc error public logic error. o The exception handler should display a prompt for the correct usage and exit. Please use the same phrasing shown in the sample runs below Calls the appropriate openFile() function (see description below) to open the relevant files. If the file name returned by openFile() is different from the one originally specified in the command line require the user to confirm to proceed with processing or to quit. Please use the prompts shown in the sample runs provided below Reads the input file line by line, and extracts the relevant payment o Check if the extracted payments are valid. Throw an exception ifthe payment is not a number characters are digits from 09 The exception class for errors is Digit public logic error. o lfthe payment is not valid, the exception handler will skip the line, and display a message Please use the same error messages shown inthe sample runs below o A sample input data file, data txt, is shown below and provided in your A3 zip file. Each line has the first 40 char for the name, then 10 char space for the payment, and the rest of the line for comments Consultation fee error International Business Management Imperial Chemical 234 Pest control chemicals Coffee machines error 34S6 Security audit MicroHeart Software Consultancy 45678 University of Silver Quartet 5678 Facility management Connection fee error Telstar Satellite Communication 670 Line 1 has an error as 12has an alphabetic I; Line 3 has an error as 34s6 has an alphabetic s; Line 6 has an error as 67o has an alphabetic o; Each line has the first 40 char for the name, then 10 char space forthe payment, and the rest of the line for comments See the sample runs below for the expected results for the data.txt file provided Calculates the total as well as the average payment. Writes the results to the output file and displays it to the screen o See the sample runs below for the expected output forthe data.txt file provided The task 3.cpp file also contains two openFile() functions, oneeach to open the input andoutput files. Each openFile() function should provide the following functionality: Attempts to open the passed in file name

Task Create a C++ application which wi read a file of daily payments, calculate the total as well as the average payment, display the results to the screen and write the results to a f e. The input and output file names should be provided as command line arguments. task 3.cpp The task 3.cpp file holds the main() function which is responsible for the following functionality: Extracts the input file name and output file name from the command line arguments. o lfthe number of command line arguments is not correct, throw an exception. The exception class is Argc error public logic error. o The exception handler should display a prompt for the correct usage and exit. Please use the same phrasing shown in the sample runs below Calls the appropriate openFile() function (see description below) to open the relevant files. If the file name returned by openFile() is different from the one originally specified in the command line require the user to confirm to proceed with processing or to quit. Please use the prompts shown in the sample runs provided below Reads the input file line by line, and extracts the relevant payment o Check if the extracted payments are valid. Throw an exception ifthe payment is not a number characters are digits from 09 The exception class for errors is Digit public logic error. o lfthe payment is not valid, the exception handler will skip the line, and display a message Please use the same error messages shown inthe sample runs below o A sample input data file, data txt, is shown below and provided in your A3 zip file. Each line has the first 40 char for the name, then 10 char space for the payment, and the rest of the line for comments Consultation fee error International Business Management Imperial Chemical 234 Pest control chemicals Coffee machines error 34S6 Security audit MicroHeart Software Consultancy 45678 University of Silver Quartet 5678 Facility management Connection fee error Telstar Satellite Communication 670 Line 1 has an error as “12”has an alphabetic “I’; Line 3 has an error as “34s6″ has an alphabetic ‘s’; Line 6 has an error as ’67o” has an alphabetic ‘o’; Each line has the first 40 char for the name, then 10 char space forthe payment, and the rest of the line for comments See the sample runs below for the expected results for the data.txt file provided Calculates the total as well as the average payment. Writes the results to the output file and displays it to the screen o See the sample runs below for the expected output forthe data.txt file provided The task 3.cpp file also contains two openFile() functions, oneeach to open the input andoutput files. Each openFile() function should provide the following functionality: Attempts to open the passed in file name

Expert Answer

 /*

* exception.h
*
* Created on: 02-Jun-2017
*      Author: Rj
*/

#ifndef EXCEPTION_H_
#define EXCEPTION_H_

#include <stdexcept>
using namespace std;

class Argc_error : public logic_error
{
public:
Argc_error(string);
};

class Readfile_error : public logic_error
{
public:
Readfile_error(string);
};

class Digit_error : public logic_error
{
public:
Digit_error(string);
};

class Writefile_error : public logic_error
{
public:
Writefile_error(string);
};

#endif /* EXCEPTION_H_ */

/*
* exception.cpp
*
* Created on: 02-Jun-2017
*      Author: Rj
*/

#include <string>
#include “exception.h”
using namespace std;

Argc_error::Argc_error(string m) : logic_error(m)
{}

Readfile_error::Readfile_error(string m) : logic_error(m)
{}

Digit_error::Digit_error(string m) : logic_error(m)
{}

Writefile_error::Writefile_error(string m) : logic_error(m)
{}

Here is the main file:

//============================================================================
// Name        : main.cpp
// Author      : Ramachandra jr
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include “exception.h”
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;

struct Payment
{
string _name;
int _payment;
string _comment;
};

const int MAX = 100;
// Payments array.
Payment *payments = new Payment[MAX]{};
int size = 0;

/**
* Reports any non numeric charaters in a string. Removes if just whitespace.
* @param {string} str – String to convert.
* @return {string} String with just numbers.
*/
string just_nums(string str)
{
string no_spaces = “”;
for (int i = 0; i < str.length(); ++i)
{
int dig = str.at(i)-48;

// If a space.
if ( dig == -16 )
{}
// 0 – 9
else if (dig > -1 && dig < 10)
no_spaces += str.at(i);
// non numeric
else
throw Digit_error(“There was a non character”);
}

return no_spaces;
}

/**
* Converts a legit number in string form to integer.
* @param {string} num – Number to convert to int.
* @return {int} Integer form of string.
*/
int str_to_int(string num)
{
const int LEN = num.length();
int tot = 0;

for (int i = 0; i < LEN; ++i)
tot += (num.at(i)-48) * pow(10,LEN-i-1);

return tot;
}

void extract_data(Payment *p1, string line)
{
stringstream ss(line);
int len = line.length();

char *name        = new char[41]{};
char *pay        = new char[11]{};
char *comment   = new char[len – 48]{};

ss.read(name, 40);
ss.read(pay, 10);
ss.read(comment, len – 49);

// char* to string
string pay_str(pay);

p1->_name = name;
try
{
pay_str = just_nums(pay_str);
p1->_payment = str_to_int(pay_str);
}
catch (Digit_error err)
{
throw Digit_error(err.what());
}
p1->_comment = comment;
}

Payment* open_file(const char *filename)
{
// file
fstream inf;
// For holding line.
string line;

// File handling.
inf.open(filename, ios_base::in);
if (! inf.is_open())
throw Readfile_error(“Unable to open file!”);

while(getline(inf, line))
{
Payment *p1 = new Payment{};
try {
extract_data(p1, line);
}
catch (Digit_error err)
{
cerr << “Error: ” << err.what() << endl;
continue;
}
payments[size] = *p1;
++size;
}

return payments;
}

int main(int argc, char *argv[]) {
Payment *pays;

try
{
// Improper arguments.
/*
* My IDE does not support command line args, you can go ahead and
* un comment lines below.
*/
//if (argc != 3)
//throw Argc_error(“Missing input / output filename(s)!”);

if (argv[1])
pays = open_file(argv[1]);
else
pays = open_file(“somefile.txt”);

for (int i = 0; i < size; ++i)
{
if (pays[i]._name.length() > 1) {
cout << i << “) “<< pays[i]._name
// To see if its addable.
<< ” – ” << pays[i]._payment
<< ” – ” << pays[i]._comment
<< endl;
}
}
}
catch (Argc_error err)
{
cerr << err.what() << endl;
cerr << “Usage: ./main <input_file> <output_file>”;
}

return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!