Inventory will not display or load
Mar 5, 2017 at 6:35pm UTC
Hey there everyone! I feel like I'm almost done with this, but I can't quite figure something out. This program is meant to read an inventory from invent.in and then print a receipt to receipt.out. Sadly, the inventory will not print to the display, and it says the products are not found. Any help is appreciated! Here is my code.
groceries.h
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
#ifndef GROCERIES_H
#define GROCERIES_H
using namespace std;
const double TAXRATE = 0.075;
// Item in grocery inventory
class Groceries
{
public :
// Constructors
Groceries() : product(-1), description("" ), price(0.0), taxable(' ' ) { }
// Groceries
Groceries(int prod, string desc, double cost, char tax) :
product(prod), description(desc), price(cost), taxable(tax) { }
// Input/output
void ReadGroceries(istream& in);
void WriteGroceries(ostream& out);
// Access functions
inline int Product() { return (product); }
inline string Description() { return (description); }
inline double Price() { return (price); }
inline char Taxable() { return (taxable); }
// Calculations
inline double Sale(int quantity) { return (price * quantity); }
inline double Tax(int quantity) {
return (taxable != 'T' ? 0.00 : Sale(quantity) * TAXRATE);
}
private :
int product; // Product code
string description; // Description
double price; // Unit price
char taxable; // T = Taxable and N = Non-taxable
};
// Grocery stockroom
class Stockroom
{
public :
// Constructor
Stockroom(int shelves);
// Output
void WriteStock(ostream& out);
// Enter food into stock
bool BuyStock(Groceries food);
// Search for product
Groceries* GroceryClerk(int prod);
//Load GroceryList from file
void LoadGroceryList(const char * Filename); // Print invent from file
private :
Groceries *pantry; // Groceries in stock
int groceries, // Number of groceries in stock
stockshelves; // Max groceries in stock
};
#endif
groceries.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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "groceries.h"
using namespace std;
// Input Groceries from in
void Groceries::ReadGroceries(istream& in)
{
in >> product >> description >> price >> taxable;
}
// Output Groceries to out
void Groceries::WriteGroceries(ostream& out)
{
out << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
out.precision(2); // Formatting for receipt
string desc = description;
int descblanks = 12 - desc.length() + 1;
for (int d = 0; d < descblanks; d++) desc += ' ' ;
out << setw(5) << product << " " << desc << setw(4) << price
<< " " << taxable;
}
// Stockroom for shelves items
Stockroom::Stockroom(int shelves)
{
pantry = new Groceries[shelves];
stockshelves = shelves;
groceries = 0;
}
// Returns true if food added to stock, false otherwise
bool Stockroom::BuyStock(Groceries food)
{
if ((groceries + 1) >= stockshelves) return (false );
for (int g = 0; g < groceries; g++)
if (food.Product() < pantry[g].Product()) {
for (int g1 = groceries++; g1 >= g; g1--)
pantry[g1 + 1] = pantry[g1];
pantry[g] = food;
return (true );
}
else if (food.Product() == pantry[g].Product()) return (true );
pantry[groceries++] = food;
return (true );
}
Groceries* Stockroom::GroceryClerk(int prod)
{
int low = 0;
int high = groceries - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (pantry[mid].Product() == prod) return (&pantry[mid]);
if (pantry[mid].Product() < prod) low = mid + 1;
else high = mid - 1;
}
return (NULL);
}
// Output all items in Stockroom to out
void Stockroom::WriteStock(ostream& out)
{
for (int g = 0; g < groceries; g++) {
pantry[g].WriteGroceries(out);
out << endl;
}
out << endl;
}
void Stockroom::LoadGroceryList(const char * FileName)
{
ifstream invent(FileName); // Read inventory
if (invent.is_open())
{
groceries = 0; //
Groceries TempGrocery;
// Read in stock until max stock value is reached
for (int count = 0; count < stockshelves; count++)
{
TempGrocery.ReadGroceries(invent);
if (!invent.eof() || !invent.fail())
{
// Save grocery data
pantry[count] = TempGrocery;
groceries++;
}
else
{
// End of file; read failed
break ;
}
}
invent.close();
}
}
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 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include "groceries.h"
using namespace std;
const char *RECEIPTS = "receipts.out" ; // File for receipts
const char *INVENT = "invent.in" ; // File for inventory
const int STOCKROOM = 30; // Max items in inventory
int main()
{
Stockroom stockroom(30);
stockroom.LoadGroceryList(INVENT); //Stockroom loads the groceries
ofstream receipts(RECEIPTS);
stockroom.WriteStock(receipts); // List inventory on the receipt
stockroom.WriteStock(cout); // List inventory on the screen
int customer = 1;
char more; // For holding whether or not there are more transactions
do {
double bought(0.00), tax(0.00);
receipts << endl << "Customer " << customer << endl;
cout << "Enter purchases for customer # " << customer++ << endl
<< "Enter product code '0' to end the transaction." << endl << endl;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
cout.precision(2);
receipts << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
receipts.precision(2);
int prod, quantity;
do {
cin >> prod;
if (prod > 0) {
Groceries *product = stockroom.GroceryClerk(prod);
cin >> quantity;
if (product == NULL) { // Product not found?
receipts << "*** Item " << prod << " not found in inventory ***" << endl;
cout << "*** Item " << prod << " not found in inventory ***" << endl;
}
else if ((quantity < 1) || (10 < quantity)) {
receipts << "*** " << quantity << " of item " << prod <<
" are not for sale ***" << endl;
cout << "*** " << quantity << " of item " << prod <<
" are not for sale ***" << endl;
}
else { // Sale can proceed
bought += product->Sale(quantity);
tax += product->Tax(quantity);
string desc = product->Description();
int descblanks = 12 - desc.length() + 1;
for (int d = 0; d < descblanks; d++)
desc += ' ' ;
receipts << desc << setw(2) << quantity << " @ " << setw(4) <<
product->Price() << setw(6) << product->Sale(quantity)
<< (product->Tax(quantity) > 0.00 ? " TX" : " " ) << endl;
cout << desc << setw(2) << quantity << " @ " << setw(4) <<
product->Price() << setw(6) << product->Sale(quantity)
<< (product->Tax(quantity) > 0.00 ? " TX" : " " ) << endl;
}
}
} while (prod != 0);
if (bought > 0.00) { // If customer made purchase
receipts << endl << " Subtotal "
<< setw(5) << bought << endl <<
" Tax "
<< setw(5) << tax << endl << endl <<
" Total "
<< setw(5) << (bought + tax) << endl << endl;
cout << endl << " Subtotal "
<< setw(5) << bought << endl <<
" Tax "
<< setw(5) << tax << endl << endl <<
" Total "
<< setw(5) << (bought + tax) << endl << endl;
}
cout << "Is there another customer? (Y/N) " ;
cin >> more;
} while (more == 'Y' || more == 'y' );
// Stop if they say 'N'
cout << "Thanks for shopping with us!" << endl;
return 0;
}
Mar 5, 2017 at 6:35pm UTC
Here is my inventory txt file:
12112 eggs 1.99 N
56312 bacon 2.99 N
82142 bread 0.89 N
96012 jam 1.49 N
42315 spatula 0.99 T
57416 waffles 1.29 N
23412 strawberries 1.49 N
Topic archived. No new replies allowed.