In: Computer Science
Prem is a loan officer, He scanned a pile of documents from his desk for different mortgage applications. Some of the loan applications are missing required documents. Write a program to help him organize the documents, and determine which ones are missing.
Every document contains metadata, including:
- document name("fileName"),
- document owner("owner"),
- document type ("docType"),
- loan application ID ("applicationId"), and
- content length ("contentLength")
your task is to find all the document types that are missing for each loan application. Every loan application must include every document type. Since you are new to Prem's work, you don't know how many document types there are in total. You should figure that from the pile yourself.
Input (stdin):
Take input from a file("myFile.txt" download it from iLearn). A sample file input is given below as an example.
A comma delimited file with the first line as field names.
Input Sample file Example:
fileName,owner,docType,applicationId,contentLength
bank_statement_1,Sunny,bank_statement,1,1000
tax_document_1,Sunny,tax_return,1,16001
tax_document_2,Jay,tax_return,2,2000
document_123,Ram,tax_return,3,1500
medical_report,Ram,medical_history,3,15000
prescription,Jason,medical_history,4,200
property_assets,Jason,bank_statement,4,4000
OutPut (stdout):
- Print out two lines for each document type. The first line is document type. The second line are the application IDs that are missing for this type of document.
- Delimit the application IDs by one space
- Sort document types in alphabetical order. Sort application IDs in numerical order.
- Print only document types when they are missing for atleast one loan application.
Sample Output :
bank_statement
2 3
medical_history
1 2
tax_return
4
Explanation:
After scanning the given file we come to know that each application should contain bank_statement, medical_history and tax_return.
Jay and Ram are missing bank_statement in their application
Sunny and Jay are missing medical_history in their application
Jason is missing tax_return in his application
since Id of sunny is 1, Jay is 2, Ram is 3 and Jason is 4.
if we write all documents in alphabetical order and people who are missing them. Then the output would look like the sample output above.
Note: The content of "myFile.txt" will be changed while testing your program. So make sure your program works for more documents too.
Please turn in a working .cpp file that takes input from "myFile.txt" and displays output to the console in the format mentioned above.
//Code Image
//Sample Input file
//Sample Output
//Code to copy
//working.cpp
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include <stdlib.h>
#include<string>
/*Use std namespace */
using namespace std;
/*Declare a class Report */
class Report
{
/*Declare variable fileName type of string */
string fileName;
/*Declare variable owner type of string */
string owner;
/*Declare variable docType type of string */
string docType;
/*Declare variable applicationId type of integer */
int applicationId;
/*Declare variable contentLength type of integer */
int contentLength;
/*Declare variable numberOfRecords type of integer */
int numberOfRecords;
public:
/*Declare readFile method */
void readFile(Report[]);
/*Declare display method*/
void display(Report[]);
/*Declare display InvalidReport*/
void InvalidReport(Report[]);
/*Declare display SortRecordNumber*/
void SortRecordNumber(Report rep[]);
};
/*Implementation of the readFile method*/
void Report::readFile(Report rec[])
{
/*Declare ifstream type variable*/
ifstream readf;
/*Open file from its Location */
readf.open("D:/myFile.txt");
/*Declare variable */
numberOfRecords = 0;
/*Store data as type of string */
string hed;
/*Declare array type of char */
char heading[100];
/*Read heading from text file */
readf >> hed;
/*Iterate the Loop up to end */
while (!readf.eof())
{
for (int x = 0; x < 5; x++)
{
/*Switch case */
switch (x)
{
case 0:
/*Extract data from file */
readf.getline(heading, 256, ',');
/*Store data into records */
rec[numberOfRecords].fileName = heading;
break;
case 1:
/*Extract data from file */
readf.getline(heading, 256, ',');
/*Store data into records */
rec[numberOfRecords].owner = heading;
//Break
break;
case 2:
/*Extract data from file */
readf.getline(heading, 256, ',');
/*Store data into records */
rec[numberOfRecords].docType = heading;
break;
case 3:
/*Extract data from file */
readf.getline(heading, 256, ',');
/*Converts data to integer */
rec[numberOfRecords].applicationId = atoi(heading);
break;
case 4:
/*Extract data from file */
readf >> rec[numberOfRecords].contentLength;
break;
}
}
//Increment the counts as numberof Records
numberOfRecords++;
}
/*file close*/
readf.close();
}
/*Implementation a function "display()" */
void Report::display(Report rec[])
{
cout << "\n Records in the file: ";
for (int x = 0; x < numberOfRecords; x++)
//Display statement
cout << rec[x].fileName << " " << rec[x].owner << " " << rec[x].docType << " " << rec[x].applicationId << " " << rec[x].contentLength;
}
/*Implementation a function "SortRecordNumber()" */
void Report::SortRecordNumber(Report rec[])
{
//Loops till end
for (int x = 0; x < numberOfRecords; x++)
{
for (int y = 0; y <
numberOfRecords - x - 1; y++)
{
//If id is
greater then
if
(rec[y].applicationId > rec[y + 1].applicationId)
{
/* Assign record to temp */
Report temp = rec[y];
rec[y] = rec[y + 1];
rec[y + 1] = temp;
}
}
}
}
/*Implementation a function "InvalidReport()" */
void Report::InvalidReport(Report rec[])
{
/*Declare variables as type of array */
int flag[100][3] = { 0,0,0,0 };
/* Declare arrays as integer type */
int bank[100], medical[100], tax[100];
//Declare variables
int cb = 0, cm = 0, ct = 0;
//Declare variable
int c = 0;
//Loops till end
for (int x = 0; x < numberOfRecords; x++)
{
//If x is 0
if (x == 0)
{
//If record is bank_statement
if (rec[x].docType == "bank_statement")
{
//Set id
flag[c][0] = rec[x].applicationId;
}
//check document type
if (rec[x].docType == "medical_history")
{
//Set record number
flag[c][1] = rec[x].applicationId;
}
//Check type
if (rec[x].docType == "tax_return")
{
//Assign id
flag[c][2] = rec[x].applicationId;
}
}
//Otherwise
else
{
//Checks id
if (rec[x].applicationId == rec[x - 1].applicationId)
{
//Checks document type
if (rec[x].docType == "bank_statement")
{
//Assign id
flag[c][0] = rec[x].applicationId;
}
//Checks document type
if (rec[x].docType == "medical_history")
{
//Assign id
flag[c][1] = rec[x].applicationId;
}
//Check document type
if (rec[x].docType == "tax_return")
{
//Assign id
flag[c][2] = rec[x].applicationId;
}
}
//Otherwise
else
{
//Increment
c++;
//Checks type
if (rec[x].docType == "bank_statement")
{
//Assign id
flag[c][0] = rec[x].applicationId;
}
//Check type
if (rec[x].docType == "medical_history")
{
//Assign value
flag[c][1] = rec[x].applicationId;
}
//Check type
if (rec[x].docType == "tax_return")
{
//Assign id
flag[c][2] = rec[x].applicationId;
}
}
}
}
//Loop until end
for (int x = 0; x <= c; x++)
{
//If value is 0
if (flag[x][0] == 0)
//Assign value
bank[cb++] = (x + 1);
//If value is 0
if (flag[x][1] == 0)
//Assign value
medical[cm++] = (x + 1);
//If value is 0
if (flag[x][2] == 0)
//Assign value
tax[ct++] = (x + 1);
}
//Display message
cout << "\n\n bank_statement\n";
//Loop until length
for (int x = 0; x < cb; x++)
{
//Display value
cout << bank[x] << ", ";
}
//Display message
cout << "\n medical_history \n";
//Loop until count
for (int x = 0; x < cm; x++)
{
//Display message
cout << medical[x] << ", ";
}
//Display message
cout << "\n tax_return\n";
//Loops until count
for (int x = 0; x < ct; x++)
{
cout << tax[x] <<
",";
}
}
//Define main method
int main()
{
//Create array of type Report
Report rec[100];
//Read the file contents
rec[0].readFile(rec);
/*Displays array of records*/
rec[0].display(rec);
/* Sort array process */
rec[0].SortRecordNumber(rec);
/*Generate document as report format */
rec[0].InvalidReport(rec);
system("pause");
//Return 0
return 0;
}