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.
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:
International Business Management l2 Consultation fee // errorLine 1 has an error as “l2”has an alphabetic 'l';
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
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:
You may also find it convenient to include separate functions to perform required conversions from string to integer and dates to strings.
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);
};
Test your program to ensure it handles the following scenarios. Compare your programs output to the sample runs provided.
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