0

hi its me again, same program new problem... been working on this all day and i just cannot get it to work.

#define INVENTORY_H
#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <fstream>
#include <vector>
#include <ctime>
using namespace std;

class Inventory
{
private:

    static const char *Error[];
    static const double Taxes;

    long Inv;
    string Description;
    double price;
    char tax;

public:
    enum error_msg{NOT_FOUND, IS_DUPLICATE, E_QUANTITY};

    const long& getInventory() const {return Inv;}
    const string& getDescription() const {return Description;}
    const double & getPrice() const {return price;}
    const char& getTax() const {return tax;}



    Inventory::Inventory() : Inv(10000), Description(""), price(0.0), tax('N'){}

    Inventory(const Inventory &Inven) {*this = Inven;}
    virtual Inventory::~Inventory() {Inv = 0; price = 0.0; tax = 'N'; Description = "";}

    void operator = (const Inventory &Inven)
    {
        Inv = Inven.getInventory();
        Description = Inven.getDescription();
        price = Inven.getPrice();
        tax = Inven.getTax();
    }

    bool operator == (const Inventory &item)
    {
        return item.getTax() == tax && item.getPrice() == price && item.getDescription() == Description && item.getInventory() == Inv;
    }

    friend bool isInventory(vector <Inventory>& Inven, const int&itemNum)
    {
        for (int i = 0; i < Inven.size(); i++)
        {
            if (Inven[i].Inv == itemNum)
                return true;

        }
        return false;

    }
    friend bool isDuplicate(vector <Inventory> & Inven, const Inventory& invCheck)
    {
        for (int i = 0; i < Inven.size(); i++)
        {
            if (Inven[i] == invCheck)
                return true;
        }
        return false;

    }

    bool isTaxable()
    {
        return tax == 'T';
    }

    friend istream& operator >> (istream& input, const Inventory &item)
    {
        operator >> (input, (Inventory &)item);
    }

    friend istream& operator >> (istream& input, Inventory &item)
    {
        if (!input.eof())
        {
            input >> item.Inv >> item.Description >> item.price >> item.tax;
        }
        return input;
    }





    static string getReceipt(double sum, double Taxes)
    {

        char totalRec[1000];

        sprintf(totalRec, "\n%s\nSub Total: %10.21f\nTaxes: %10.21f\n%s\nTotal: %10.21f\n%s\n", sum, Taxes, sum+Taxes);
        string rec = totalRec;
        return rec;

    }

    friend ostream& operator << (ostream& out, Inventory &item)
    {
        out << setw(5) << item.getInventory() << " " << setw(12) << item.getDescription() << " " << setw(8) << fixed << setprecision(2) << item.getPrice() << " " << item.getTax();
        return out;
    }

    static string getItem(Inventory &item, const int& Quantity, double& Tax, double Sum)
    {
        string dataReturn;
        char Item[300];
        memset(Item, 0, sizeof(Item));
        string d = item.getDescription();
        double p = item.getPrice();
        string T = item.isTaxable() ? "T" : "N";

        sprintf(Item, "%12.12s %3d @ %6.21f %-2.2s %7.21f", d.c_str(), Quantity, p, T.c_str(), Quantity * p);
        Sum += Quantity * p;

        if (item.isTaxable())
        {
            Tax += Taxes * (Quantity * p);

        }
        dataReturn = Item;
        return dataReturn;


    }

    static string getError(int index, long Inv)
    {
        string errorReturn;
        char error[256];
        if (index >= NOT_FOUND || index <= E_QUANTITY)
        {
            sprintf(error, Error[index], Inv);
            errorReturn = error;
        }
        return errorReturn;
    }

    static string search4Item(vector<Inventory>& inv, const long& InvenNum, const int& Quantity, double& Tax, double& Sum)
    {
        string dataReturn;
        bool bFound = false, bDuplicate = false, bQuantity = Quantity >0;
        int Found1 = 0;

        if (!bQuantity)
        {
            dataReturn = getError(E_QUANTITY, InvenNum);

        }
        for (int i = 0; i < inv.size(); i++)
        {
            if (inv[i].getInventory() == InvenNum)
            {
                if (bFound)
                {
                    if (dataReturn.length() > 0)
                    {
                        dataReturn += "\n";
                    }
                    dataReturn += getError(IS_DUPLICATE, InvenNum);
                    bDuplicate = true;

                }
                else
                {
                    bFound = true;
                    Found1 = i;
                }
            }
        }
        if (!bDuplicate && bQuantity && bFound)
        {
            dataReturn = getItem(inv[Found1], Quantity, Tax, Sum);

        }
        if (!bFound)
        {
            dataReturn = getError(NOT_FOUND, InvenNum);
        }
        return dataReturn;
    }


};

heres the main cpp file

#include "Inventory.h"
#include <iostream>
#include <istream>
#include <fstream>
using namespace std;


void main()
{


    filebuf Invent1;
    Invent1.open("inventory.txt", ios::in);

    filebuf Receipt;
    Receipt.open("receipt.txt", ios::out);

    istream inv(&Invent1);
    ostream rec(&Receipt);

    vector <Inventory> v;
    Inventory item;

    while (!inv.eof())
    {
        inv >> item;
        if (item.getDescription() != "")
        {
            v.push_back(item);
        }

    }

    for (int i = 0; i <  v.size(); i++)
    {
        cout << v[i] << endl;


    }

    char input;
    long invenNum = 0;
    int quantity = 0;
    double tax = 0;
    double sum = 0;
    string out = "";


    do
    {
        input = 0;
        invenNum = 0;
        quantity = 0;

        cout << "Enter the inventory number and the quantity of each seperated by a space: " << endl;
        cin >> invenNum >> quantity;
        //int price = item.getPrice();

        if (quantity != 0 && invenNum !=0)
        {
            input = 'y';

            out = item.search4Item(v, invenNum, quantity, tax, sum);
            cout << out << endl;
            rec << out << endl;


        }
        else 
        {
            rec << Inventory::getReceipt(sum, tax);
            cout << Inventory::getReceipt(sum, tax);
            cout << endl << "would you like to help another customer? y or n" << endl;
            cin>>input;
            if (tolower(input == 'y'))
            {
                sum =0;
                tax = 0;

            }
}

        cin.ignore();           

    } 

    while (tolower(input) == 'y');
    system("pause");










}

and heres another small cpp file for doing the errors

#include "Inventory.h"
const double Inventory::Taxes = 0.075;

const char *Inventory::Error[] = 
  {
  "%ld is not in inventory",
  "%ld has duplicate inventory #",
  "%ld QUANTITY less than 1"
  };

if you compile it you will notice that for whatever reason it lets you type in the item numbers and quantities. it works and tells you how much it is for how much you buy, and then when you get to the point where you want to end the transaction and see the sub total, taxes, and total, it doesnt work. if there are taxes, the compiler crashes, if there arent taxes it brings 0's accross the board for all of it. can anyone tell me whats wrong?

1
Contributor
1
Reply
3
Views
4 Years
Discussion Span
Last Post by Clouded One
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.