I wished I had you and the community here when I was first assigned this last week, because you are able to explain this in a way that makes more sense (and more practical out in the real world) than how I'm being led by my instructor and the textbook that is from Pearson.
So, this is the part where I must include the remaining .cpp and .h files so that I can be 100% sure that nothing is being misunderstood and mistyped so that I can finally understand how these files were meant to talk to each other and run the whole darn thing from the main.cpp. Only then will I be able to actually get going with my final project with better understanding on what that is requiring me to do, but I'm just rambling now so here we go.
Here's main.cpp:
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
|
#include "cashregister.h"
#include "InventoryItem.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int intItem;
double dblTax = 0.06;
double dblCost = 0.0;
int intUnits = 0;
int intUnitsAvailable = 0;
char charRunSwitch;
const int ITEMS = 5;
//this added all items to inventory by using constructor 3
InventoryItem inventory[ITEMS] =
{
InventoryItem("Adjustable Wrench", 9.95, 10),
InventoryItem("Screwdriver", 4.50, 20),
InventoryItem("Pliers", 3.00, 35),
InventoryItem("Ratchet", 9.95, 10),
InventoryItem("Socket Wrench", 5.00, 7)
};
do
{
cout << "# \t "<< setw(12) << "Inventory Item \t\tCost \tQty On Hand" << endl;
cout << "---------------------------------------------------------------------------" << endl;
for (int count = 0; count < ITEMS; count++)
{
cout << count + 1 << " \t";
cout << setw(16) << inventory[count].getDescription() << " \t\t";
cout << inventory[count].getCost() << " \t";
cout << inventory[count].getUnits() << endl ;
}
cout << endl << "Which item above is being purchased? " << endl;
cin >> intItem;
while (intItem < 1 || intItem > 5) //input validation
{
cout << "Please enter a number within the range 1 - 5: ";
cin >> intItem;
}
dblCost = inventory[intItem - 1].getCost(); //get cost of unit to be purchased
intUnitsAvailable = inventory[intItem - 1].getUnits(); //get number of available units
cout << "How many units? ";
cin >> intUnits;
while (intUnits < 0 || intUnits > intUnitsAvailable) //input validation
{
cout << "Please enter a valid quantity: ";
cin >> intUnits;
}
inventory[intItem - 1].setUnits(intUnitsAvailable - intUnits); //adjust inventory
// cout << "dblcost = " << dblCost << endl; -- used for debugging
cashregister Register(dblCost, intUnits, dblTax); //add new instance of cashregister class using constructor 2
cout << fixed << showpoint << setprecision(2);
cout << "Subtotal (includes 30% fee): $" << Register.getSubTotal() << endl;
cout << "Sales Tax: $" << Register.getTax() << endl;
cout << "Total: $" << Register.getTotal() << endl << endl;
cout << "Do you wish to purchase another item? (Enter Y or N)";
cin >> charRunSwitch;
while (charRunSwitch != 'N' && charRunSwitch != 'n' && charRunSwitch != 'Y' && charRunSwitch != 'y') //input validation
{
cout << "Please enter a valid answer (Y or N): ";
cin >> charRunSwitch;
}
}
while(charRunSwitch == 'Y' or charRunSwitch == 'y' ); //check switch
system("pause");
return 0;
};
|
And here's the other .cpp:
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
|
#include "cashregister.h"
cashregister::cashregister() //default constructor
{
subTotal = 0;
total = 0;
tax = 0.06;
}
cashregister::cashregister(double dblCost, int intUnits, double dblTax) //constructor that accepts value during implementation
{
subTotal = (1.3*dblCost)*intUnits,
tax = subTotal*dblTax;
total = tax + subTotal;
}
//mutator functions begin
void cashregister::setSubTotal(double dblSubTotal)
{
subTotal = dblSubTotal;
}
void cashregister::setTotal(double dblTotal)
{
total = dblTotal;
}
void cashregister::setTax(double dblTax)
{
tax = dblTax;
}
//mutator functions end
//begin accessor functions
double cashregister::getSubTotal() const
{
return subTotal;
}
double cashregister::getTotal() const
{
return total;
}
double cashregister::getTax() const
{
return tax;
}
//end accessor functions
|
And it's .h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
using namespace std;
class cashregister //: public InventoryItem
{
private:
double subTotal;
double total;
double tax;
public:
cashregister();
cashregister(double dblCost, int intUnits, double dblTax);
void setSubTotal(double dblSubtotal);
void setTotal(double dblTotal);
void setTax(double dblTax);
double getSubTotal() const;
double getTotal() const;
double getTax() const;
};
|
So, now that we have all five files here, in main, there's a problem when it's trying to create the five constructors within the object array with the following error:
main.cpp(26-30): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'InventoryItem' |
Based on what you have told me so far, are they still valid enough to be kept, and now this just needs to be solved with more code changes? Again, according to my instructor, these .cpp and .h answer files are supposed to work
as they are. He can compile them fine and apparently can run them without changing the createDescription function, as per your corrected suggestion. And supposedly these files have been used for several years, which quite frankly, does not give me confidence because languages and digital technology have been rapidly changing. Shutting up now so I can see what can be done.