(Solved Homework): 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;
}

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents