1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void getdata(string& name, int& accountNumber, int& inFeet, double& costinFeet, int& exFeet, double& costexFeet, double& errorinFeet, double& errorcostinFeet, double& errorexFeet, ifstream& inFile, ofstream& outFile, ofstream& errorFile);
void calculation(int inFeet, double incost, int exFeet, double excost, double& totalin, double& totalex, double& discount, double& total);
void sendData(const string& name, int num, int inFeet, double incost, int exFeet, double excost, double totalin, double totalex, double discount, double total, ofstream& outFile);
int main()
{
ifstream inFile("lab6.txt");
ofstream outFile("outfile.txt");
ofstream errorFile("errorFile.txt");
if (!outFile || !errorFile || !inFile)
return(cout << "Cannot open file, terminating the program. "), 1;
while (true) {
string name;
int accountNumber {}, inFeet {}, exFeet {};
double costinFeet {}, costexFeet {}, errorinFeet {}, errorcostinFeet {}, errorexFeet {};
getdata(name, accountNumber, inFeet, costinFeet, exFeet, costexFeet, errorinFeet, errorcostinFeet, errorexFeet, inFile, outFile, errorFile);
if (!inFile)
break;
if (errorinFeet + errorcostinFeet + errorexFeet == 0) {
// process data here - display data for testing purposes
//cout << name << " " << accountNumber << " " << inFeet << " " << costinFeet << " " << exFeet << " " << costexFeet << '\n';
double totalin {}, totalex {}, discount {}, total {};
calculation(inFeet, costinFeet, exFeet, costexFeet, totalin, totalex, discount, total);
sendData(name, accountNumber, inFeet, costinFeet, exFeet, costexFeet, totalin, totalex, discount, total, outFile);
}
}
}
void getdata(string& name, int& accountNumber, int& inFeet, double& costinFeet, int& exFeet, double& costexFeet, double& errorinFeet, double& errorcostinFeet, double& errorexFeet, ifstream& inFile, ofstream& outFile, ofstream& errorFile)
{
if (inFile >> name >> accountNumber >> inFeet >> costinFeet >> exFeet >> costexFeet) {
errorinFeet = inFeet <= 0;
errorcostinFeet = costinFeet <= 0;
errorexFeet = exFeet <= 0;
if (errorinFeet) {
errorFile << "inFeet error: " << name << ' ' << accountNumber << ' ' << inFeet << ' ' << costinFeet << ' ' << exFeet << '\n';
}
if (errorcostinFeet) {
errorFile << "costinFeet error: " << name << ' ' << accountNumber << ' ' << inFeet << ' ' << costinFeet << ' ' << exFeet << '\n';
}
if (errorexFeet) {
errorFile << "exFeet error: " << name << ' ' << accountNumber << ' ' << inFeet << ' ' << costinFeet << ' ' << exFeet << '\n';
}
}
}
void calculation(int inFeet, double incost, int exFeet, double excost, double& totalin, double& totalex, double& discount, double& total)
{
totalin = inFeet * incost;
totalex = exFeet * excost;
total = totalin + totalex;
if (total > 1000)
discount = total * .10;
}
void sendData(const string& name, int num, int inFeet, double incost, int exFeet, double excost, double totalin, double totalex, double discount, double total, ofstream& outFile)
{
outFile << "Customer name : " << name << '\n';
outFile << "Account number: " << num << '\n';
outFile << "Interior square feet: " << inFeet << '\n';
outFile << "Exterior cost per square feet: " << incost << '\n';
outFile << "Exterior square feet: " << exFeet << '\n';
outFile << "Exterior cost per square feet: " << excost << '\n';
outFile << "Total interior cost: " << totalin << '\n';
outFile << "Total exterior cost: " << totalex << '\n';
outFile << "Discount: " << discount << '\n';
outFile << "Total: " << total << '\n';
}
|