Background Information

bool isDigit()

The < cctype> 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 < iostream>
#include < cctype>
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.
    • If the number of command line arguments is not correct, throw an exception. The exception class is Argc_error : public logic_error.
    • 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.
    • Check if the extracted payments are valid. Throw an exception if the payment is not a number all characters are digits from 09. The exception class for errors is Digit_error : public logic_error.
    • 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.
    • 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.
    • 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.
    • string openFile(ifstream& in, string str): Input file exception class is Readfile_error : public logic_error.
    • 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 programs 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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.