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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <vector>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
class shoppingItem {
int quantity;
double cost;
string name;
public:
//constructors
shoppingItem();
shoppingItem(int, double, const string&);
//"getter" functions
int getQuantity() const;
double getCost() const;
string getName() const;
//"compound getter"
double getTotalCost() const;
//functions
double maxMinTotal(const vector<shoppingItem>&, shoppingItem&, shoppingItem&);
void sortV(vector<shoppingItem>&);
};
int main(int argc, char** argv) {
vector<shoppingItem> shoppingCart;
shoppingItem SI, SI_min, SI_max;
int temp_int;
double temp_dbl, cart_total;
string l_name, f_name, temp_str;
ifstream fin;
ofstream fout;
if(argc != 3){
cout << "Requires two command line arguments" << endl;
return -1;
}
fin.open("case6_file.txt");
if(fin.fail()){
cout << "Failed to open file" << endl;
return -1;
}
fin.open(argv[1]);
fout.open(argv[2]);
if(fin.fail() || fout.fail()){
cout << "Failed to open a file" << endl;
return -1;
}
shoppingCart.clear();
while(fin >> temp_int){
if(temp_int != -1){
fin >> temp_dbl >> temp_str;
SI = shoppingItem(temp_int, temp_dbl, temp_str);
shoppingCart.push_back(SI);
} else {
fin >> f_name >> l_name;
f_name[0] = toupper(f_name[0]);
l_name[0] = toupper(l_name[0]);
// output name, quantity, cost, and total cost for each item in a shopper's cart
for(int i = 0; i < shoppingCart.size(); i++){
fout << setw(15) << left << fixed << shoppingCart[i].getName() << " "
<< right << setw(3) << shoppingCart[i].getQuantity() << " "
<< right << setw(6) << setprecision(2) << shoppingCart[i].getCost() << " "
<< right << setw(7) << setprecision(2) << shoppingCart[i].getTotalCost() << " "
<< endl;
}
// output cheapest/most expensive items, total cost of cart, and sort the cart
//HERE IT IS ~~~~~~ sortV and maxMinTotal are giving me the error
sortV(shoppingCart);
cart_total = maxMinTotal(shoppingCart, SI_min, SI_max);
fout << "cheapest item = " << SI_min.getName() << endl <<
"most expensive item = " << SI_max.getName() << endl <<
"total cost = " << setprecision(2) << cart_total << endl <<
"shopper = " << f_name << " " << l_name << endl;
shoppingCart.clear();
}
}
fin.close();
fout.close();
return 0;
}
// default constructor
shoppingItem::shoppingItem(){
}
// initialization constructor
shoppingItem::shoppingItem(int q, double c, const string& n){
quantity = q;
cost = c;
name = n;
}
//getter function for quantity
int shoppingItem::getQuantity() const{
return quantity;
}
//getter function for cost
double shoppingItem::getCost() const{
return cost;
}
//getter function for name
string shoppingItem::getName() const{
return name;
}
//"compound getter" function that returns the total amount spent on an item type
double shoppingItem::getTotalCost() const{
return cost * quantity;
}
// function returns the total cost of a shopper's cart and gives values to cheapst/most expensive items
double shoppingItem::maxMinTotal(const vector<shoppingItem>& v, shoppingItem& n, shoppingItem& x){
int i, max = -1, min = 2000, min_pos, max_pos;
double total = 0.0, temp_dbl;
for(i = 0; i < v.size(); i++){
temp_dbl = v[i].getCost();
total += v[i].getTotalCost();
if(temp_dbl > max){
max = temp_dbl;
max_pos = i;
}
else if(temp_dbl < min){
min = temp_dbl;
min_pos = i;
}
}
n = v[min_pos];
x = v[max_pos];
return total;
}
// function sorts a shopping cart in order of least-greatest cost per item
void shoppingItem::sortV(vector<shoppingItem> & v){
bool sorted = false;
int i;
shoppingItem temp;
do{
sorted = true;
for(i = 1; i < v.size(); i++){
if(v[i].getCost() < v[i-1].getCost()){
sorted = false;
v[i] = temp;
v[i] = v[i-1];
v[i-1] = temp;
}
}
}while(sorted == false);
}
|