Inventory program help.

For this program, I have to create an Inventory class with the private member variables itemNumber, quantity, cost, and totalCost. I have to have public functions including a default constructor, a second constructor that accepts itemNumber, quantity, and cost as arguments; mutators; and accessors. Then I have to write a simple program that demonstrates the class.

Here are the files:

Problem 13.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
#include "Inventory.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int itemNumber,
		quantity;
	double cost,
		   totalCost;

	cout << "Enter the Item Number: ";
	cin >> itemNumber;
	while (itemNumber < 0)
	{
		cout << "Error - Enter a positive value for the Item Number: ";
		cin >> itemNumber;
	}
	cout << "Enter the Quantity of the item: ";
	cin >> quantity;
	while (quantity < 0)
	{
		cout << "Error - Enter a positive value for the Quantity of the item: ";
		cin >> quantity;
	}
	cout << "Enter the Cost of the item: $";
	cin >> cost;
	while (cost < 0)
	{
		cout << "Error - Enter a positive value for the Cost of the item: $";
		cin >> cost;
	}

	cout << "----------------------------------" << endl;

	Inventory inventoryA(itemNumber, quantity, cost);

	totalCost = inventoryA.getTotalCost();
	itemNumber = inventoryA.getItemNumber();
	cost = inventoryA.getCost();
	quantity = inventoryA.getQuantity();
	cout << "Item Number: " << itemNumber << endl;
	cout << "Quantity: " << quantity << endl;
	cout << "Cost: $" << cost << endl;
	cout << "Total Cost: $" << fixed << setprecision(2) << totalCost << endl << endl;
	
	return 0;
}




Inventory.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
#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory
{
	private:
		int itemNumber,
			quantity;
		double cost,
			   totalCost;
	public:
		Inventory()
		{
			itemNumber = 0;
			quantity = 0;
			cost = 0;
			totalCost = 0;
		}
		Inventory(int itemNumber, int quantity, double cost)
		{
			itemNumber = getItemNumber();
			quantity = getQuantity();
			cost = getCost();
			setTotalCost(quantity, cost);
		}

		void setItemNumber(int)
		{
			itemNumber = itemNumber;
		}
		void setQuantity(int)
		{
			quantity = quantity;
		}
		void setCost(double)
		{
			cost = cost;
		}
		void setTotalCost(int, double)
		{
			totalCost = quantity * cost;
		}

		int getItemNumber()
		{
			return itemNumber;
		}
		int getQuantity()
		{
			return quantity;
		}
		double getCost()
		{
			return cost;
		}
		double getTotalCost()
		{
			return totalCost;
		}
};
#endif  





The output I get always shows some really random numbers, such as 7883933849400000000000000.00 or something for the totalCost.

It's probably something really stupid on my part, but I can't see what I've done... any help would be really appreciated.
1
2
3
4
5
6
7
Inventory(int itemNumber, int quantity, double cost)
{
	itemNumber = getItemNumber();
	quantity = getQuantity();
	cost = getCost();
	setTotalCost(quantity, cost);
}
Using the get functions will not work because that will just return the uninitialized value of the member variables. The variables that are being assigned to are the parameters. If you want the to differentiate between the parameters and the members you could use different names or put this-> in front of the variable names to specify that you mean the members.
1
2
3
4
5
6
7
Inventory(int itemNumber, int quantity, double cost)
{
	this->itemNumber = itemNumber;
	this->quantity = quantity;
	this->cost = cost;
	setTotalCost(quantity, cost);
}
Or you could assign the values in the constructor initialization list.
1
2
3
4
5
6
7
Inventory(int itemNumber, int quantity, double cost)
:	itemNumber(itemNumber),
	quantity(quantity),
	cost(cost)
{
	setTotalCost(quantity, cost);
}


The arguments to setTotalCost is never used inside the function so there is no real point in letting setTotalCost take any arguments.
Last edited on
1
2
3
4
5
6
7
Inventory(int itemNumber, int quantity, double cost)
{
	itemNumber = getItemNumber();
	quantity = getQuantity();
	cost = getCost();
	setTotalCost(quantity, cost);
}


You set cost (The parameter, not the class's member) to getCost(), which returns the UNINITIALIZED value of cost. Then you try to setTotalCost using this value.

Do this:
1
2
3
4
5
6
7
Inventory(int itemNumber, int quantity, double cost)
{
	itemNumber = getItemNumber();
	quantity = getQuantity();
	inv::cost = cost;
	setTotalCost(quantity, cost);
}


Or even better, rename the parameter to avoid confusion.
Both of your suggestions where a great help, thank you both so much.
Topic archived. No new replies allowed.