Need help with C++ functions and classes code!

Hi, I am an EXTREME beginner in C++ programming and I wish for some help on where to start for my code for my program assignment. I will post the code in another comment.
This is the assignment:

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.
Last edited on
This is the code I have so far, I do not know where to begin with this.

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
generating the random number for the SKU

What is SKU ?
1
2
3
4
void setItemNumber(int)
{
	itemNumber = itemNumber;
}

This doesn't do what you want. It takes an unnamed and unused argument, then it does this->itemNumber = this->itemNumber, which does nothing. You want:
1
2
3
4
void setItemNumber(int n)
{
	itemNumber = n;
}


You need to make similar changes to the other set() methods.

There's no need to store the totalCost. You should just compute the value in getTotalCost(). If you store it then you must update the value whenever the cost or quantity changes, which complicates the code.

Inside main(), I think you should just have the Inventory object and a temporary object for inputting the values.

Putting this all together:
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
#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;
    }
    Inventory(int newItemNumber, int newQuantity, double newCost)
    {
	itemNumber = newItemNumber;
	quantity = newQuantity;
	cost = newCost;
	// setTotalCost(quantity, cost);
    }

    void setItemNumber(int n)
    {
	itemNumber = n;
    }
    void setQuantity(int q)
    {
	quantity = q;
    }
    void setCost(double c)
    {
	cost = c;
    }

    int getItemNumber()
    {
	return itemNumber;
    }
    int getQuantity()
    {
	return quantity;
    }
    double getCost()
    {
	return cost;
    }
    double getTotalCost()
    {
	return cost * quantity;
    }
};

int
main()
{
    Inventory information;
    int i;
    double d;
    
    cout << "Enter the Item Number: ";
    cin >> i;
    while (i < 0) {
	cout << "Please enter a positive value for the Item Number: ";
	cin >> i;
    }
    information.setItemNumber(i);
    
    cout << "Enter the Quantity of the item: ";
    cin >> i;
    while (i < 0) {
	cout << "Please enter a positive value for the Quantity of the item: ";
	cin >> i;
    }
    information.setQuantity(i);
    
    cout << "Enter the Cost of the item: ";
    cin >> d;
    while (d < 0) {
	cout << "Please enter a positive value for the Cost of the item: ";
	cin >> d;
    }
    information.setCost(d);

    cout << "The Item Number is: " << information.getItemNumber() << endl;
    cout << "The Quantity is: " << information.getQuantity() << endl;
    cout << "The Cost is: " << information.getCost() << endl;
    cout << "The Total Cost is: " << information.getTotalCost() << endl;

    return 0;
}


What is SKU ?
It is "Stock Keeping Unit." This is the bar code printed on most packages. http://www.investopedia.com/terms/s/stock-keeping-unit-sku.asp
Okay, that works great! Although there are a few things I wish to add/change within the code.
I believe a loop that asks the user for the category, the item, the price, and the number in stock can perhaps be added? So all that is required is the modifications in variables. That way the validation for the input is within the class.
Topic archived. No new replies allowed.