/*******************************************************************************************
08rev1.cpp
read data into an array of objects, then display them and some calculations
*******************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "safearray.h"
//prototypes
void readFrom(string file, safearray& list);
float subTotal(const safearray& list);
void showList(const safearray& list);
void main() {
safearray goods;
string filename;
cout << "Enter the filename to read: ";
cin >> filename;
readFrom(filename, goods);
float subtotal, total;
subtotal = subTotal(goods);
float taxRate, tax;
cout << "Enter the tax rate (as a percent): ";
cin >> taxRate;
tax = subtotal * taxRate/100.0;
total = subtotal + tax;
showList(goods);
cout << "----------------------------------" << endl
<< "Subtotal: $" << subtotal << endl
<< "Tax (" << taxRate << "%): $" << tax << endl
<< "Total: $" << total << endl;
}
float subTotal(const safearray& list) {
float sum = 0.;
for (int i = 0; i < list.count(); i++)
sum += list[i].price;
return sum;
}
void showList(const safearray& list) {
for (int i = 0; i < list.count(); i++) {
cout << list[i].desc << " (" << list[i].part << ") $" << list[i].price << endl;
}
}
void readFrom(string file, safearray& list) {
ifstream fin(file.c_str());
for (int i = 0; i < list.maxsize; i++) {
int P;
if(fin >> P) {
list[i].part = P;
fin >> list[i].desc >> list[i].price;
}
else
break;
}
}