Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Assignment 3 Functional Decomposition This assignment is an EXTENSIO

ID: 3909863 • Letter: P

Question

Programming Assignment 3 Functional Decomposition This assignment is an EXTENSION of Programming Assignment 2 The basic requirements of this assignment are to create functions as indicated, determine the function type and parameters necessary, code the function calls, and relocate the necessary code from main and place in the respective function. In addition to basic the requirements of Programming Assignment 2, modify your solution as follows: 1. Both the input and output files may Create a value returning function to open files. be opened using one function. The function must contain the prompt for the user to enter the input file name. Request the name of the input file and the output file from the user. The error file name may be a literal (i.e, hardcoded). The value returned to the calling module must indicate a failure for any of the files or a success for ALL files an open fails, the calling module (main) should display a message and exit the program. This function should be called from main The name of the ifstream and ofstream identifiers/parameters in main may not be the same as the ifstream and ofstream identifiers/parameters in the file open function 2. Create and call a function to read and process the data. In this function, you may call the other functions outlined below. Remember that you need to read from the input file and write to the output files when you process the data. Parameters may be any name of your choice. This function should be called from main Create and call a function to close all data files. You may call the function from main or from the "read and process" function when you've completed all processing. Parameters may be any name of your choice 3. 4. Create and call a function to returm the agent name and hourly rate. Think about and incorporate any error processing or return values in this function. Parameters may be any name of your choice. 5. Create and call a value returning function that returns the amount due. This will require information about the current customer such as low-income indicator

Explanation / Answer

Answer: See the code below for functions 1,2 and 3:

-------------------------------------------

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

//function to open file
bool open_files(ifstream& in,ofstream& out)
{
   string infile_name; //name of input file
   string outfile_name; //name of output file

   //read file names from user
   cout<<"Enter name of input file:";
   getline(cin,infile_name);
   cout<<"Enter name of output file:";
   getline(cin,outfile_name);

   cout<<infile_name<<endl;
   cout<<outfile_name<<endl;

   //handlers for files
   in.open(infile_name.c_str(),ifstream::in); //open input file
   out.open(outfile_name.c_str(),ofstream::out); //open output file
   if(in.is_open() && out.is_open())
       return true;
   return false;
}

//function to read and process data
void read_and_process_data(ifstream& in, ofstream& out)
{
   string line;
   //read file line by line assuming text file
   while(getline(in,line))
   {
       out<<line<<endl;
   }
   //close files once over
   close_files(in,out);
}

//function to close file
void close_files(ifstream& in, ofstream& out)
{
   in.close(); //closes input file
   out.close(); //closes output file
}
int main() {
   ifstream infile; //for input file
   ofstream outfile; //for output file
   if(!open_files(infile,outfile))
   {
       cerr<<"ERROR: Can not open file"<<endl;
       return EXIT_FAILURE;
   }
   else
   {
       read_and_process_data(infile,outfile);
   }
   return 0;
}

-----------------------------------------------------

Note: For functions 4 and 5, information about format of input data and previous assignment is required which is not provided.

Output:

--------------------------

Enter name of input file:./input.txt
Enter name of output file:./output.txt
./input.txt
./output.txt
ERROR: Can not open file

-----------------------------------------