Question: I need help with my C++ program. The question says for Function 1: Read the data for one job from a file (see data below).
I need help with my C++ program. The question says for Function 1: Read the data for one job from a file (see data below). Validate that none of the numbers are negative. Return the data from good records to main and write error records onto a separate file.
My program is:
#include
#include
#include
using namespace std;
//Prototypes
void output(ofstream &oFile, string name, int ID, double interiorCost, double exteriorCost, double total);
void calculation(double intArea, double intCost, double extArea, double extCost, double &interiorCost, double &exteriorCost, double &discount, double &total, double &totalAmount);
//to check whether the input value is positive or not
bool checkInput(int number, double num1, double num2, double num3, double num4);
//main funcation
int main()
{
string name;
int ID;
double intArea=0, extArea=0;
double intCost=0, extCost=0;
ifstream inFile;
ofstream oFile;
ofstream eFile;
//declarartion for input, output, error files.
string fileName, outfile="output.txt", errorfile="error.txt";
//open the file name
inFile.open("Lab6DATA.txt");
//throw error if file opening has error
if (!inFile)
{
cout << "Error opening file ";
exit(1);
}
oFile.open (outfile);
//error if file opening has error
if (!oFile)
{
cout << "Error opening output file ";
inFile.close();
exit(1);
}
eFile.open (errorfile);
//error if file opening has error
if (!eFile)
{
cout << "Error opening error file ";
inFile.close();
oFile.close();
exit(1);
}
oFile << fixed;
eFile << "Invlaid data entered ";
//Read inputfile
while (inFile >> name >> ID >> intArea >> intCost >> extArea >> extCost)
{
if (checkInput(ID, intArea, intCost, extArea, extCost) == false)
{
eFile << name <<"\t"< continue; } double discount = 0; double interiorCost = 0; double exteriorCost = 0; double total = 0; double totalAmount = 0; //calculate the total calculation(intArea, intCost, extArea, extCost, interiorCost, exteriorCost, discount, total, totalAmount); //output to out file // Discount and final price output(oFile, name, ID, interiorCost, exteriorCost, total); } //close the files inFile.close(); oFile.close(); eFile.close(); return 0; } //Calculations void calculation(double intArea, double intCost, double extArea, double extCost, double &interiorCost, double &exteriorCost, double &discount, double &total, double &totalAmount){ //Calculate interiorcost interiorCost = intArea * intCost; //calculate the exterior Cost exteriorCost = extArea * extCost; //calculate the total total = interiorCost + exteriorCost; //calculate the discount if (total > 1000) { discount = total *0.1; } //calculate the total after the discount totalAmount = total - discount; } //Checking for negatives bool checkInput(int number, double num1, double num2, double num3, double num4) { if (num1 < 0 || num2 < 0 || num3 < 0 || num4 < 0 || number <= 0) { return false; } return true; } void output(ofstream &oFile, string name, int ID, double interiorCost, double exteriorCost, double discount, double total, double totalAmount) { oFile< oFile< oFile< oFile< oFile< oFile< oFile< } I need to put everything that's in the while into a function. I have done the part where it says to check for negative numbers but I need to read the data that's in infile and output data to the error file. My output void function inst work either so help would be nice. ABC 1234 400 3.50 850 5.50 DEF 1345 100 5.25 200 2.75 GHI 2346 200 10.00 0 0.0 JKL 4567 375 4.00 50 4.00 MNO 5463 200 -5.0 150 3.00 PQR 679 0 0.0 100 3.50 STU 6879 100 0.0 -100 0.0 VWX 7348 0 0.0 750 0.0 XYZ 9012 -24 5.00 -50 5.00 This is the data file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
