Need help with C++ functions & classes code!

\Hi, this is a problem I am currently stuck on and I do not know where to begin with. Any help will be greatly appreciated!

The Wong Chinese Imports company has asked you to create a program to be used by their warehouse staff to maintain inventory. We have now completed the first two phases of the project, and the company is very pleased with the results.

Currently, we have several variables needed for manipulating the inventory. We now want to clean up that code and move it into functions and classes.

Using the variables created for the application, create a class object called Inventory. Our variables should be private within that class. In addition, set up public member functions for setting and getting the values for those private variables. There should be two constructors: a default constructor to set all values to zero or empty (for string variables), and a parameter constructor for setting the values when declared.

The class will also include functions for calculating the total value of the inventory and for generating the random number for the SKU.

We will still be using a loop to ask the user for the category, the item, the price, and the number in stock, so all that is required is the modifications in variables. Validation for the input should be within the class.

The output will remain as we have previously done it, so, again, modifications are needed for the variables only.

Last edited on
Please show the code that you're starting from.
This is how I have my code at the moment. Still needs a lot of work but I think I have a few things right.
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;

class Inventory
{
	private:
		int itemNumber;
		int quantity;
		double cost;
	    double totalCost;
	public:
		Inventory()
		{
			itemNumber = 0;
			quantity = 0;
			cost = 0;
			totalCost = 0;

		}
		Inventory(int newItemNumber, int newQuantity, double newCost)
{
  itemNumber = newItemNumber;
  quantity = newQuantity;
  cost = newCost;
  setTotalCost(quantity, cost); // this can be called as quantity and cost have now been initialized.
}

		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;
		}
};


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

	cout << "Enter the Item Number: ";
	cin >> itemNumber;
	while (itemNumber < 0)
	{
		cout << "Please enter a positive value for the Item Number: ";
		cin >> itemNumber;
	}
	cout << "Enter the Quantity of the item: ";
	cin >> quantity;
	while (quantity < 0)
	{
		cout << "Please 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 << "Please enter a positive value for the Cost of the item: ";
		cin >> cost;
	}

	Inventory information(itemNumber, quantity, cost);

	totalCost = information.getTotalCost();
	itemNumber = information.getItemNumber();
	cost = information.getCost();
	quantity = information.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: " << totalCost << endl;

	return 0;
}

Last edited on
Lines 34,38,42,46: You're setting the member variable to itself. You need to name your argument and set the member variable to the argument.

Line 97: You have only a single inventory instance. i.e. It can hold only one item. You need an array, or better yet a std::vector.

generating the random number for the SKU.

I see no code for generating a random SKU as per the assignment.

We will still be using a loop to ask the user for the category, the item, the price, and the number in stock

I see no such loop.

Validation for the input should be within the class.

I see no such validation in your class.



Topic archived. No new replies allowed.