Ask C/C++ Expert


Home >> C/C++

Assignment

Assignment consists of one task.

The steps to copy the compilation messages and sample runs for the task_3.txt file are software dependent:

- If you are using Codelite, the compilation messages can be copied and pasted with the usual crtl-c and crtl-v. For output of sample runs, right click the title bar of the command window, select EDIT/MARK and then highlight the output. With the output selected, right click the title bar of the command window, select EDIT/COPY. Position your cursor to a (new) text file opened in a text editor such as Notepad, and crtl-v will paste the copied output to the text file.

- If you are using MinGW, right click the title bar of the command window; select EDIT/MARK and then high light the output. With the output selected, right click the title bar of the command window, select EDIT/COPY. Position your cursor to a (new) text file opened in a text editor such as Notepad, and crtl-v will paste the copied output to the text file.

- If you are using Linux, you can cut and paste the output on the terminal window into a text such as vim.

NOTE: MinGW 4.7.1 will be used to compile and test your assignment. Please ensure that your solution will compile without errors. If necessary comment out erroneous code.

Other files you should download:

o sample_isdigit.cpp
o data.txt
o data_c.txt

Background information
bool isdigit()
The library provides a function isdigit() to test if a char is one of the decimal digits from 0 -9. The function will return false when the char is not one of the digits; and true when char is a digit. The code below for Sample_isdigit.cpp illustrates how isdigit() can be used:

#include
#include
using namespace std; int main(){
char a = 'A'; char b = '3'; char c = ' ';

if (isdigit(a)){
cout << a << " is a digit!" << endl;
} else {
cout << a << " is not a digit!" << endl;
}

if (isdigit(b)){
cout << b << " is a digit!" << endl;
} else {
cout << b << " is not a digit!" << endl;
}

if (isdigit(c)){
cout << c << " is a digit!" << endl;
} else {
cout << c << " is not a digit!" << endl;
}

return 0;
}
/*
Sample output c\>a
A is not a digit!
3 is a digit!
is not a digit!
*/

NOTE: isdigit() will consider a space to be a non-digit.

Task

Create a C++ application which will 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 file. 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 If the 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 if the payment is not a number - all characters are digits from 0-9. The exception class for errors is Digit_error : public logic_error.
o If the payment is not valid, the exception handler will skip the line, and display a message. Please use the same error messages shown in the 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.

International Business Management l2 Consultation fee // error
Imperial Chemical 234 Pest control chemicals
National Home Appliances 34S6 Coffee machines // error
MicroHeart Software Consultancy 45678 Security audit
University of Silver Quartet 5678 Facility management

Telstar Satellite Communication 67O Connection fee // error
- Line 1 has an error as "l2"has an alphabetic ‘l';
- 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 for the 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 for the data.txt file provided.

The task_3.cpp file also contains two openFile() functions, one each to open the input and output files. Each openFile() function should provide the following functionality:

- Attempts to open the passed in file name.

- If there is an error when opening the provided file name the function should throw an exception, passing whatever information is required to the exception handler. Please use the error messages shown in the sample runs below.

- The exception handler should display an appropriate error message and prompt the user to provide a new file name, again please use the prompts shown in the sample runs below. The exception handler should then recursively call openFile() passing the new file name. The openFile() function will continue to be recursively called until a file is successfully opened. In practice you would put a limit on the number of potential calls, in order to avoid infinite recursion. For this assignment please disregard this problem.

- The relevant exception class for the two openFile() functions are shown below.
o string openFile(ifstream& in, string str): Input file exception class is Readfile_error : public logic_error.
o string openFile(ofstream& out, string str): Output file Exception class is Writefile_error : public logic_error.

- Return the name of the opened file.

You may also find it convenient to include separate functions to perform required conversions from string to integer and dates to strings.

exception.cpp, exception.h
The required exception class definitions are shown below. Implement the required exception functions in exception.cpp. Include code in exception.h to prevent multiple inclusion of the file.

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

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

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

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

Testing
Test your program to ensure it handles the following scenarios. Compare your program's output to the sample runs provided.

- error in command line argument list

- error in input file name

- error in the payment field of some lines in the data file

Record your sample output and compilation message(s) to task_3.txt. Please note: the dates displayed in the sample runs below are the actual dates at run time; your results will be different.

Sample runs for task_3.cpp:

- Incorrect usage of command line arguments:
C:\csc2402>g++ -Wall task_3.cpp exception.cpp

C:\csc2402>a
Usage: app_name data_file_name output_file_name.

- Cannot open input file, do not proceed with processing:
C:\csc2402>a dat.txt out.txt
Cannot open data file dat.txt. Please input the correct data file name: daat.txt Cannot open data file daat.txt. Please input the correct data file name: dtta.txt Cannot open data file dtta.txt. Please input the correct data file name: data.txt Cannot open dat.txt, data.txt opened instead.
Do you want to proceed? ('y' to proceed)n
Cannot proceed further. Program is closing down.

- Cannot open input file, proceed with processing, invalid data in file:
C:\csc2402>a dat.txt out.txt
Cannot open data file dat.txt. Please input the correct data file name: data.txt
Cannot open dat.txt, data.txt opened instead. Do you want to proceed? ('y' to proceed)y Number contains non digits: l2
Current line will be skipped!

Number contains non digits: 34S6 Current line will be skipped!

Number contains non digits: 67O Current line will be skipped!

Date: 05/12/15 16:57:40
Total payment: $51590 Average payment: $17196.67

- Cannot open output file: [test by setting the permissions on your output file to Read only]
C:\csc2402>a data_c.txt out.txt
Cannot open data file out.txt. Please input the correct data file name: out2.txt
Cannot open out.txt, out2.txt opened instead. Do you want to proceed? ('y' to proceed)y Date: 05/18/15 13:45:32
Total payment: $51590 Average payment: $17196.67

- Sample run for a perfect data file - i.e. remove invalid lines from the data.txt file:
C:\csc2402>a data_c.txt out.txt Date: 05/12/15 16:57:40
Total payment: $51590 Average payment: $17196.67

Attachment:- Data.rar

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M92326450
  • Price:- $45

Priced at Now at $45, Verified Solution

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As