Function not working.

Can I know why my diplayItem didn't work?


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

using namespace std;

class box {
	public:
		void open();
		void removeItem();
		void storeItem(int x);
		void close();
		void displayItem(int x);
		box();
	private:
		int item;
		bool boxOpen;
		bool empty;
};



int main()
{
	box myBox;

	myBox.open();
	myBox.storeItem(50);
	myBox.removeItem();
	myBox.storeItem(150);
	myBox.displayItem();
		

	return 0;
}

	box::box(){
		empty=true;
		boxOpen=false;
	}

	void box::open()
	{
		if (boxOpen)
				cout << "Box already open\n";
		else
		{
			boxOpen=true;
			cout << "Box is now open\n";
		}
	}


	void box::removeItem()
	{
		if (!boxOpen)
			cout << "Box is closed, please open first.\n";
		else if (!empty)
		{
			cout << "item " << item << " is removed from box";
			empty=true;
		}

		else
			cout << "No item to remove.\n";
	}

	void box::storeItem(int x)
	{
		if (!boxOpen)
			cout << "Box is closed, please open first.\n";
		else if (!empty)
			cout << "Cannot store new item, box is full.\n";
		else
		{
			item=x;
			cout << "\nItem " << x << " is stored.\n";
			empty = false;
		}
	}

	void box::close()
	{
		if (!boxOpen)
			cout << "Box is already closed\n";
		else
		{
			boxOpen=false;
			cout << "Box is now closed\n";
		}
	}
	
	void box::displayItem(int x)
	{
		item=x;
		if(empty)
			cout<<"The box is empty. No item in box";
		else
			cout<<"The current value of item "<< x <<endl;
			
	}
What do your error messages tell you?

You didn't pass it any argument. When you declare it as: function(int) it means you should pass in some int (or anything that can be casted to it, e.g. double).
Topic archived. No new replies allowed.