Inventory Class returning a negative inventory

Problem; Item Number (identification), Quantity, and Cost of item are all displaying large negative values at the end of program.

Prompt; Design an Inventory class that can hold information for an item in a retail store’s inventory.

ItemNumber -> An int that holds the item’s number.
Quantity -> An int that holds the quantity of the item on hand.
Cost -> A double that holds the wholesale per-unit cost of the Item

setItemNumber -> Accepts int argument & copies it into the ItemNumber
setQuantity -> Accepts int argument & copies it into the Quantity
setCost -> Accepts double argument & copies it into the Cost
getItemNumber -> Returns the value in ItemNumber
getQuantity -> Returns the value in Quantity
getCost -> Returns the value in Cost
getTotalCost -> Computes and returns the TotCost

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
#include <iostream>
using namespace std;

class Inventory
{
private:
	int ItemNumber;
	int Quantity;
	double Cost;
	double TotCost;

public:
	Inventory()
	{
		ItemNumber = 0;
		Quantity = 0;
		Cost = 0;
		TotCost;
	}

	Inventory(int EndItemNumber, int EndQuantity, double EndCost)
	{
		EndItemNumber = getItemNumber();
		EndQuantity = getQuantity();
		EndCost = getCost();
		setTotCost(Quantity, Cost);
	}

	void setItemNumber(int)
	{
		ItemNumber = ItemNumber;
	}

	void setQuantity(int)
	{
		Quantity = Quantity;
	}

	void setCost(double)
	{
		Cost = Cost;
	}

	void setTotCost(int, double)
	{
		TotCost = Quantity*Cost;
	}

	int getItemNumber()
	{
		return ItemNumber;
	}

	int getQuantity()
	{
		return Quantity;
	}

	double getCost()
	{
		return Cost;
	}

	double getTotCost()
	{
		return TotCost;
	}
};

int main()
{
	int ItemNumber;
	int Quantity;
	double Cost;
	double TotCost;

	cout << "Please enter the Item Number: ";
	cin >> ItemNumber;
	while (ItemNumber < 0)
	{
		cout << "Only enter positive values for the Item Number: ";
		cin >> ItemNumber;
	}

	cout << "Please enter the Quantity of this Item: ";
	cin >> Quantity;
	while (Quantity < 0)
	{
		cout << "Only enter positive values for the Quantity: ";
		cin >> Quantity;
	}

	cout << "Please enter the Cost of this Item: ";
	cin >> Cost;
	while (Cost < 0)
	{
		cout << "Only enter positive values for the Cost: ";
		cin >> Cost;
	}

	Inventory Info(ItemNumber, Quantity, Cost);

	TotCost = Info.getTotCost();
	ItemNumber = Info.getItemNumber();
	Cost = Info.getCost();
	Quantity = Info.getQuantity();

	cout << "The Item Number is: " << ItemNumber << endl;
	cout << "The Quantity is: " << Quantity << endl;
	cout << "The Cost is: " << Cost << endl;
	cout << "The Total Cost is: " << TotCost << endl;

	system("pause");
	return 0;
}
Your constructor doesn't use any values supplied to it?
1
2
3
4
5
6
7
8
9
10
11

	Inventory(int EndItemNumber, int EndQuantity, double EndCost)
	{
//Get garbage ..
		EndItemNumber = getItemNumber();
		EndQuantity = getQuantity();
		EndCost = getCost();
		setTotCost(Quantity, Cost);
	}

 


should be
1
2
3
4
5
6
7
8
Inventory(int EndItemNumber, int EndQuantity, double EndCost)
: ItemNumber(EndItemNumber)
, Quantity(EndQuantity)
, Cost(EndCost)
{
//now you have initialize your data !
}
	
Last edited on
The default values should be 0, then we supply new values for the program to use. I'm sorry, I a little novice. This is the entire prompt I was given, not sure if it will help...

7. Inventory Class
Design an Inventory class that can hold information for an item in a retail store’s inventory.
The class should have the following private member variables.

Variable Name Description
itemNumber An int that holds the item’s number.
quantity An int that holds the quantity of the item on hand.
cost A double that holds the wholesale per-unit cost of the item

The class should have the following public member functions

Variable Name Description
itemNumber An int that holds the item’s number.
quantity An int that holds the quantity of the item on hand.
cost A double that holds the wholesale per-unit cost of the item

Member Function Description
default constructor Sets all the member variables to 0.
constructor #2 Accepts an item’s number, quantity, and cost as arguments.

Calls other class functions to copy these values into the appropriate member variables. Then calls the setTotalCost function.

setItemNumber Accepts an int argument and copies it into the itemNumber member variable.
setQuantity Accepts an int argument and copies it into the quantity member variable.
setCost Accepts a double argument and copies it into the cost member variable.
getItemNumber Returns the value in itemNumber.
getQuantity Returns the value in quantity.
getCost Returns the value in cost.
getTotalCost Computes and returns the totalCost.

Demonstrate the class by writing a simple program that uses it. This program should validate
the user inputs to ensure that negative values are not accepted for item number, quantity, or cost.
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

class Inventory
{
	unsigned int m_itemNumber;
	unsigned int m_quantity;
	unsigned int m_cost;
public:
	Inventory()
		: m_itemNumber(0)
		, m_quantity(0)
		, m_cost(0)
	{
	}
	Inventory(int itemNumber, int quantity, int cost)
		: m_itemNumber(abs(itemNumber))
		, m_quantity(abs(quantity))
		, m_cost(abs(cost))
	{
	}
	void setItemNumber(int number)
	{
		m_itemNumber = abs(number);
	}
	void setQuantity(int quantity)
	{
		m_quantity = abs(quantity);
	}
	void setCost(int cost)
	{
		m_cost = abs(cost);
	}
	unsigned int getItemNumber() const
	{
		return m_itemNumber;
	}
	unsigned int getQuantity() const
	{
		return m_quantity;
	}
	unsigned int getCost() const
	{
		return m_cost;
	}
	unsigned int getTotalCost() const
	{
		return m_quantity * m_cost;
	}

};

int main(int argc, char* argv[])
{

	Inventory instance(-1,2,-6);
	instance.getCost();
	instance.getItemNumber();
	instance.getQuantity();
	instance.getTotalCost();

	return 0;
}
Okay, I got my quantity, cost, and item number to display properly. But now the total cost is displaying a huge negative number. This is my current code

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
#include <iostream>
using namespace std;

class Inventory
{
private:
	int ItemNumber;
	int Quantity;
	double Cost;
	double TotCost;

public:
	Inventory()
	{
		ItemNumber = 0;
		Quantity = 0;
		Cost = 0;
		TotCost;
	}

	Inventory(int EndItemNumber, int EndQuantity, double EndCost)
		: ItemNumber(EndItemNumber)
		, Quantity(EndQuantity)
		, Cost(EndCost)
	{
	}

	void setItemNumber(int)
	{
		ItemNumber = ItemNumber;
	}

	void setQuantity(int)
	{
		Quantity = Quantity;
	}

	void setCost(double)
	{
		Cost = Cost;
	}

	void setTotCost(int, double)
	{
		TotCost = Quantity*Cost;
	}

	int getItemNumber()
	{
		return ItemNumber;
	}

	int getQuantity()
	{
		return Quantity;
	}

	double getCost()
	{
		return Cost;
	}

	double getTotCost()
	{
		return TotCost;
	}
};

int main()
{
	int ItemNumber;
	int Quantity;
	double Cost;
	double TotCost;

	cout << "Please enter the Item Number: ";
	cin >> ItemNumber;
	while (ItemNumber < 0)
	{
		cout << "Only enter positive values for the Item Number: ";
		cin >> ItemNumber;
	}

	cout << "Please enter the Quantity of this Item: ";
	cin >> Quantity;
	while (Quantity < 0)
	{
		cout << "Only enter positive values for the Quantity: ";
		cin >> Quantity;
	}

	cout << "Please enter the Cost of this Item: ";
	cin >> Cost;
	while (Cost < 0)
	{
		cout << "Only enter positive values for the Cost: ";
		cin >> Cost;
	}

	Inventory Info(ItemNumber, Quantity, Cost);

	TotCost = Info.getTotCost();
	ItemNumber = Info.getItemNumber();
	Cost = Info.getCost();
	Quantity = Info.getQuantity();

	cout << "The Item Number is: " << ItemNumber << endl;
	cout << "The Quantity is: " << Quantity << endl;
	cout << "The Cost is: " << Cost << endl;
	cout << "The Total Cost is: " << TotCost << endl;

	system("pause");
	return 0;
}
Might it be because you never initialize or assign to the class member TotCost?
If I try to set TotCost=0; initially I still get the same result.
I know I need to call the setTotCost function, this is why the TotCost in my main program and class are never set. But where do I call it?? I'm lost in a sea of code...=///
I know I need to call the setTotCost function, this is why the TotCost in my main program and class are never set.


It doesn't even make sense to have a setTotCost function.

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
class Inventory
{
private:
    int ItemNumber;
    int Quantity;
    double Cost;

public:
    Inventory() : ItemNumber(0), Quantity(0), Cost(0) {}

    Inventory(int EndItemNumber, int EndQuantity, double EndCost)
        : ItemNumber(EndItemNumber)
        , Quantity(EndQuantity)
        , Cost(EndCost)
    {
    }

    // Note that we name the parameters and actually use them:
    void setItemNumber(int id) { ItemNumber = id; }
    void setQuantity(int amount) { Quantity = amount; }
    void setCost(double amount) { Cost = amount; }

    int getItemNumber() const { return ItemNumber; }
    int getQuantity() const { return Quantity; }
    double getCost() const { return Cost; }

    // Calculated on demand:
    double getTotCost() const { return Quantity * Cost; }
};



Topic archived. No new replies allowed.