need help with Grocery Item class and functions

Mar 19, 2016 at 9:06pm
Hello I'm running into some issues with my grocery store program. The class GroceryItem is supposed to define private values to hold information about the GroceryItem object. Currently, When I call the dataEntry function, the program displays the functions, but does not allow for input for the current function before showing the next one. I could really just use some pointers about how to correctly use classes to get and set values. I believe I am on the right track but I just need some more instruction.

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
#include <iostream>

using namespace std;

class GroceryItem{
private: int stockNumber;
		 double price = 0.0;
		 int quantity;
		 double totalValue;
		 double setPrice();
		 int setStockNum();
		 int setQuantity();
		 void setTotalValue();
public:
	void dataEntry();
	void displayValues();

};
int GroceryItem::setStockNum(){
	int stock = 0;
	cout << "Enter the stock number for the grocery item: ";
	while (!(stock >= 1000 || stock <= 9999))
	{
		cout << "Stock Number: ";
		cin >> stock;
	}
	stockNumber = stock;
	return stockNumber;
}
double GroceryItem::setPrice(){
	double x = 0.0;
	cout << "Enter the price of the item: ";
	while (!(x > 0))	{
		cout << "Please enter a positive number for price!";
		cin >> x;
	}
	price = x;
	return price;
}
int GroceryItem::setQuantity(){
	int x = 0;
	cout << "Enter the quantity in stock: ";
	while (!(x > 0)){
		cout << "Please enter a positive number for quantity!";
		cin >> x;
	}
	quantity = x;
	return quantity;
}
void GroceryItem::setTotalValue(){
	totalValue = (quantity * price);
}
void GroceryItem::dataEntry(){
	setStockNum();
	system("pause");
	setPrice();
	system("pause");
	setQuantity();
	system("pause");
	setTotalValue();
}
void GroceryItem::displayValues(){
	cout << "Stock number: " << stockNumber;
	cout << "\nItem price: " << price;
	cout << "\nQuantity on hand: " << quantity;
	cout << "\nTotal value of item: " << totalValue;
}


int main(){
	GroceryItem Milk;
	Milk.dataEntry();
	Milk.displayValues();
	system("pause");
	return 0;
}
Last edited on Mar 19, 2016 at 9:06pm
Mar 19, 2016 at 10:46pm
> while (!(stock >= 1000 || stock <= 9999))
tell me one number that satisfies that condition.
You initialized `stock' with 0, that's less than 9999, so the condition fails.
Topic archived. No new replies allowed.