/******************************************************************************************* P9rev1.cpp read data into an array of objects, then display them and some calculations Uses "safe" array to hold objects. *******************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; #include "safearray.h" //prototypes void readDBase(string file, safearray& list); void readInput(string file, safearray& list); void XRef(const safearray& DB, safearray& order); float subTotal(const safearray&); void showList(const safearray&); void main() { string filename; cout << "Enter the INVENTORY/DATABASE FILE to read: "; cin >> filename; safearray DBase; readDBase(filename, DBase); cout << "Enter the SCANNER FILE to read: "; cin >> filename; safearray Input; readInput(filename, Input); float taxRate; cout << "Enter the tax rate (as a percent): "; cin >> taxRate; cout << endl; // run by the scanner data to see what is a valid item in the order XRef(DBase, Input); //perform necessary calculations float subtotal = subTotal(Input); float tax = subtotal * taxRate/100.0; float total = subtotal + tax; //display the "invoice" showList(Input); cout << "----------------------------------" << endl << "Subtotal: $" << subtotal << endl; cout.unsetf(ios::fixed); cout.precision(6); cout << "Tax (" << taxRate; cout.setf(ios::fixed); cout.precision(2); cout << "%): $" << tax << endl << "Total: $" << total << endl; } // ------- Load the database file from disk and init counters ------- void readDBase(string file, safearray& list) { ifstream fin(file.c_str()); for (int i = 0; i < safearray::maxsize; i++) { int P; if (fin >> P) { list[i].part = P; fin >> list[i].desc >> list[i].price; } else break; } } // ------- Load the list of scanned part numbers from disk void readInput(string file, safearray& list) { ifstream fin(file.c_str()); for (int i = 0; i < safearray::maxsize; i++) { int P; if (fin >> P) list[i].part = P; else break; } } // ------- Cross-reference the list and increment the count of items that are ordered void XRef(const safearray& DB, safearray& order) { //go thru and see what's been ordered for (int i = 0; i < order.count(); i++) //for each item ordered for (int j = 0; j < DB.count(); j++) //find it's corresponding entry if (order[i].part == DB[j].part) { // in the database list order[i].desc = DB[j].desc; //copy its info over order[i].price = DB[j].price; break; } } // ------- parse the DBase for the subtotal of all items ------- float subTotal(const safearray& list) { float sum = 0.; for (int i = 0; i < list.count(); i++) sum += list[i].price; return sum; } // ------- print out the individual items ------- void showList(const safearray& list) { //currency formatting cout.setf(ios::fixed); cout.precision(2); for (int i = 0; i < list.count(); i++) //for each item in the list cout << list[i].desc << " (" << list[i].part << ") $" << list[i].price << endl; }