Uninitialized Local Variable Error

Problem; I'm getting an "Uninitialized Local Variable" error from VS. My problem before was the program didn't call the setTotalCost function so the main program didn't have anything to go off. I think I fixed that but now it seems I've created more errors along the way...

Prompt (for clarification); 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. Callsother 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
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 = 0;
	}

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

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

	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;
}
1
2
3
4
void setItemNumber(int)
{
	ItemNumber = ItemNumber;
}

This just sets the current object's ItemNumber to ItemNumber and achieves nothing. You need to add a name in the parameter and set ItemNumber to the variable passed in.

Like so...
1
2
3
4
void setItemNumber(int itemNum)
{
	ItemNumber = itemNum;
}


Also, you should initialise doubles like this
double num = 0.0;
Last edited on
Topic archived. No new replies allowed.