Need help with array of type struct

Hi! I need some help understanding what my assignment is telling me to do, and how I can implement it. My teacher says that my information in the catalogue (item numbers and prices) should all be in one array of the type struct. (struct products). I'm not sure how to put everything into an array and calculate the individual prices and total.

Thanks in advance!

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

using namespace std;

struct products
{
	int item, quantity;
	double cost;
};


void func (int, int);

int main(int argc, char** argv) {
	char answer;
	bool Repeat = true;
	int prodNum;
	products cat [100];
	cout << cat;
	
	cout << "Please select a product: " << endl << endl;
	cout << "Product 1 - $3.08\n\nProduct 2 - $4.52\n\nProduct 3 - $10.87\n\n"
		 << "Product 4 - $4.49\n\nProduct 5 - $9.68" << endl;
	cin >> prodNum;
	cout << "Please enter the quantity of this item: "<< endl << endl;
		
		
	if ((prodNum <= 0) || (prodNum >= 6))
	{
		cout << "You entered an invalid number.";
	}
		
		cout << endl;
		
	cout << "Would you like to enter another product? (Enter Y to continue): ";
		cin >> answer;

		if (answer == 'Y' || answer == 'y')
		{
			Repeat = true;
		}

		else 
			return false;

	
	return 0;
}

void func (int som, int oth)
{
 		
	
}
closed account (zybCM4Gy)
Well....

Well, you've created a catalog with 100 spaces. You've not put anything into that catalog though. So you'll probably want to go ahead and do that for three items....

(Reduce 100 to 3 temporarily).

1
2
3
//...
    products cat [3];
//... 


Now at the very least you'll want to put some items in there. So each item in the array can hold an Item, a Quantity and a cost.

So lets say we have some items. We have 3 books, 2 cups and a toilet.

Each of these, is an item in the array, three different objects, three array places.

1
2
3
cat[0].quantity = 3; //Three books
cat[0].item = ; //I'm assuming you want a SKU code here personally I'd have used a name but meh
cat[0].cost = 1.50; 


Congrats we now have an item.

Now as for getting an individual price...well... does the cost refer to each item or is it a total?

It's the total, you'll need...

 
answer = cat[0].cost / 3


But you can skip the /3 if it's each.

I highly recommend messing with: http://www.cplusplus.com/doc/tutorial/structures/
Thanks! I never knew how to do that! Ok I now have it going smoothly. My last problem is that I would like to be able to get the total of each product and then output a final total. For example, I would like to be able to calculate the total of 2 of product one and 3 of product 4. The only thing my program is doing right now is telling me the total price of a certain quantity of one product is, instead of all products. Any idea how I could do that? Can I do it in the same function or maybe open a new function?
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
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
#include <iostream>

using namespace std;

struct products
{
	int item, quantity;
	double cost, itemTotal;
};


void func (int, int);

int main(int argc, char** argv) {
	char answer;
	bool Repeat = true;
	int prodNum, quant;
	
	cout << "Please select a product number: " << endl << endl;
	cout << "Product 1 - $3.08\n\nProduct 2 - $4.52\n\nProduct 3 - $10.87\n\n"
		 << "Product 4 - $4.49\n\nProduct 5 - $9.68\n\n" << endl;
	cout << "Product: ";
	cin >> prodNum;
	cout << "\nPlease enter the quantity of this item: "<< endl << endl;
	cin >> quant;
	cout << endl;
		
		
	if ((prodNum <= 0) || (prodNum >= 6))
	{
		cout << "You entered an invalid number.";
	}
		
	cout << endl;
	
	func(prodNum, quant);
		
	cout << "Would you like to enter another product? (Enter Y to continue): ";
		cin >> answer;

		if (answer == 'Y' || answer == 'y')
		{
			Repeat = true;
		}

		else 
			return false;

	
	return 0;
}

void func (int item, int quan)
{
 		products cat [5];
 		double total;
 		
 		cat[0].itemTotal = 0;
 		cat[1].itemTotal = 0;
 		cat[2].itemTotal = 0;
 		cat[3].itemTotal = 0;
 		cat[4].itemTotal = 0;
 		
 		//Info for product 1
 		if (item == 1)
 		{
 			cat[0].quantity = quan;
			cat[0].item = 1;
			cat[0].cost = 3.08;
			cat[0].itemTotal = cat[0].cost * cat[0].quantity;
 		}
 		//Info for product 2
		if (item ==2)
		{
			cat[1].quantity = quan;
			cat[1].item = 2;
			cat[1].cost = 4.52;	
			cat[1].itemTotal = cat[1].cost * cat[1].quantity;
		}
		//Info for product 3
		if (item == 3)
		{
			cat[2].quantity = quan;
			cat[2].item = 3;
			cat[2].cost = 10.87;
			cat[2].itemTotal = cat[2].cost * cat[2].quantity;
		}
		//Info for product 4
		if (item == 4)
		{
			cat[3].quantity = quan;
			cat[3].item = 4;
			cat[3].cost = 4.49;
			cat[3].itemTotal = cat[3].cost * cat[3].quantity;
		}
		//Info for product 5
		if (item == 5)
		{
			cat[4].quantity = quan;
			cat[4].item = 5;
			cat[4].cost = 9.68;	
			cat[4].itemTotal = cat[4].cost * cat[4].quantity;
		}
		
		
		total = cat[0].itemTotal + cat[1].itemTotal + cat[2].itemTotal + cat[3].itemTotal + cat[4].itemTotal;
		
		cout << "The total is: " << total << endl << endl;
		
		
	
}
Topic archived. No new replies allowed.