Class definition of an Item class.

My objective is to write the code for the class definition of an Item class. The main() function should create an array of 5 Item objects. Then the main() must loop through the array allowing the user to enter data for each item(but not a quantity). As each item description is entered, use the setDesc() mutator to assign the entry to the description data member and use the increment() method to add 1 to the quantity.
Assume that the user will enter 5 different items
Finally, the main() function should loop through the array of items showing each description and quantity

My problem seems to be with the main() function. I don't quite understand how I can go about allowing the user to add their own items and assigning it to the array. I feel I am close though, here's what I have so far

Thanks,

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
  #include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Item {

	private:
		string description;
		int quantity;


	public:
		void setDesc(string); // mutator for description
		Item() { quantity = 0; }; //initialize quantity to 0
		void increment(); //adds 1 to quantity
		void display(); //void

int main(){

	objects[5];
	cout << "Enter 5 items: " << endl;
        string item;

	for(int i = 0; i < 5; i++)
	{
                cin >> item;
		objects[i] = description; //idk quite what I was doing here



}

void Item::setDesc(string item)
{
	description = item;
	increment();
}

void Item::increment()
{
	quantity++
}

void Item::display()
{
	cout << description << " " << quantity << endl;
}
there were various missing/misaligned braces in your program, this is a working version but you may have to shape it a bit further depending upon your final requirements:
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
 #include <iostream>
#include <string>
//#include <cstdlib>
using namespace std;

class Item {

	private:
		string description;
		int quantity;


	public:
		void setDesc(string item); // mutator for description
		Item() { quantity = 0; }; //initialize quantity to 0
		void increment(); //adds 1 to quantity
		void display(); //void
};

int main(){

	Item objects[5];
	cout << "Enter 5 items: " << endl;


	for(int i = 0; i < 5; i++)
	{
           string item;
           getline(cin, item);
		objects[i].setDesc(item); //idk quite what I was doing here
		objects[i].increment();
	}
}

void Item::setDesc(string item)
{
	description = item;
	//increment();
}

void Item::increment()
{
	++quantity;
}

void Item::display()
{
	cout << description << " " << quantity << endl;
}
Topic archived. No new replies allowed.