Hello, in my program, my output in the summary section is incorrect and was wondering if I could get any pointers on which direction I should head? Basically, the program keeps track of factories and orders a certain number of items depending on what you request.
This is what my output looks like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Total items ordered in month 1
grommet 225
widget 120
Items ordered by factory
Amherst
widget 20
Norfolk
grommet 150
widget 100
Northampton
grommet 75
Total items ordered in month 2
grommet 375
widget 295
Items ordered by factory
Amherst
widget 20
Norfolk
grommet 300
widget 200
Northampton
grommet 75
widget 75
Total items ordered in month 3
flange 26
grommet 80
widget 151
Items ordered by factory
Amherst
widget 40
Norfolk
Northampton
flange 15
grommet 75
Pittsfield
flange 11
grommet 5
widget 111
Summary 0
Items ordered by factory
Amherst
widget 40
Norfolk
grommet 300
widget 200
Northampton
flange 15
3 factories ordered 555 items of 4 different kinds.
Items ordered by factory
Amherst
widget 40
Amherst
widget 40
Norfolk
grommet 300
widget 200
Norfolk
grommet 300
widget 200
Northampton
flange 15
grommet 75
Northampton
flange 15
6 factories ordered 1185 items of 9 different kinds.
Items ordered by factory
Amherst
widget 40
Amherst
widget 40
Amherst
widget 40
Norfolk
Norfolk
grommet 300
widget 200
Norfolk
grommet 300
widget 200
Northampton
flange 15
grommet 75
Northampton
flange 15
grommet 75
Northampton
flange 15
Pittsfield
flange 11
grommet 5
widget 111
10 factories ordered 1442 items of 15 different kinds.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I know the problem is in one of these two files but not exactly sure where.
factories.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
|
#include "factory.h"
#include <algorithm>
using namespace std;
Factory::Factory ()
: numOrders(0), maxOrders(4)
{
orders = new Order[maxOrders];
}
Factory::Factory (std::string nm)
: numOrders(0), maxOrders(4), name(nm)
{
orders = new Order[maxOrders];
}
// Clear all orders for this factory
void Factory::clearOrders()
{
numOrders = 0;
}
// Place an order for some number of items.
// If a prior order has been placed for these, this additional
// order adds to that prior one. The Item is also updated to reflect
// the change in total number of that item ordered by all factories.
void Factory::orderItems (Item* itemToOrder, int numToOrder)
{
int k = 0;
while (k < numOrders && orders[k].item->name != itemToOrder->name) //while k is less then numOrders and the order item name is not the itemToOrder name
++k; //add one to k
if (k < numOrders && orders[k].item == itemToOrder) //If k is less than the number of orders and the order item is the item to order
{
// This factory already has an order for some of these items
orders[k].amount += numToOrder; //then add the number of the order to the orders that have already been requested
}
else //or else
{
// A new item for this factory - add it
if (numOrders >= maxOrders) //if the numOrders is greater then or equal to the maxOrders
{
Order* newOrders = new Order[2*maxOrders]; //then create a new order
copy (orders, orders+numOrders, newOrders); //and copy the old orders into a new order
delete [] orders; //delete the old orders
orders = newOrders; //put the neworders into the orders array (using operator overloading)
maxOrders *= 2; //maxOrders = maxOrders * 2
}
orders[numOrders] = Order(itemToOrder, numToOrder); //
int k = numOrders; //k equals the number of orders
while (k > 0 && orders[k].item->name < orders[k-1].item->name) //while k is greater than zero and
{
swap (orders[k], orders[k-1]);
--k;
}
++numOrders;
}
// Update the total ordered of this item
itemToOrder->amount += numToOrder;
}
// Access to Orders (in alphabetic order by item name)
Factory::OrderPosition Factory::begin() const
{
return orders;
}
Factory::OrderPosition Factory::end() const
{
return orders+numOrders;
}
Factory::OrderPosition Factory::find(Item* it) const
{
OrderPosition pos = begin();
while (pos != end() && pos->item != it)
++pos;
return pos;
}
|
factories.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
|
#ifndef FACTORY_H
#define FACTORY_H
#include "item.h"
#include "order.h"
class Factory {
public:
Factory ();
Factory (std::string name);
// Factory name attribute
std::string getName() const {return name;}
void setName (std::string nm) {name = nm;}
// Clear all orders for this factory
void clearOrders();
// Place an order for some number of items.
// If a prior order has been placed for these, this additional
// order adds to that prior one. The Item is also updated to reflect
// the change in total number of that item ordered by all factories.
void orderItems (Item* itemToOrder, int numToOrder);
// Access to Orders (in alphabetic order by item name)
typedef Order* OrderPosition;
OrderPosition begin() const;
OrderPosition end() const;
// Find the position of the order corresponding to the
// given item. If no such order exists, return end().
OrderPosition find (Item*) const;
private:
std::string name;
// Dynamically allocated array of Order - do NOT change this
// choice of data structures.
Order* orders;
int maxOrders;
int numOrders;
};
#endif
|
The problem is in one of those two files on top here. The output is wrong in the summary. For example, Amherst should have had 80 widgets, Norfolk should have had 450 grommets and 300 widgets and so fourth.
Other files that might be needed to understand parts of my problem are:
order.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
|
#ifndef ORDER_H
#define ORDER_H
#include <string>
class Item;
// An order placed by a factory for some number of items
struct Order {
Item* item; // the item being ordered
int amount; // how many are needed
Order (Item* it, int amt)
: item(it), amount(amt)
{}
Order()
: item(0), amount(0)
{}
bool operator== (const Order& right) const
{
return item == right.item
&& amount == right.amount;
}
};
#endif
|
item.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
|
#ifndef ITEM_H
#define ITEM_H
#include <string>
// An item tracked by the JIT inventory system.
// Although this is currently a very simple structure, we anticipate
// that it may become considerably more complicated and considerably
// larger in later releases of the system.
struct Item {
std::string name;
int amount;
Item (std::string nm, int amt)
: name(nm), amount(amt)
{}
Item()
: amount(0)
{}
bool operator== (const Item& right) const
{
return name == right.name
&& amount == right.amount;
}
};
#endif
|
I have a few more files but I think this is all you should need to help me get on the move. Any and all guidance will be greatly appreciated.