Array Struct

How would I get the array for products_amount to show only the information for the first item in the array.

meaning, if I inputted something like
name: screw driver
part number 555
cost 5.59

and then inserted another item's information, the program would only return the information for the first element of the array.


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

using namespace std;

struct Product
{
	string description;	// product description
	int partNum;	// Part Number
	double cost;	// Product cost
};


int main()
{
	const int Products_Amount = 2;	// array size and number of products
	Product items[Products_Amount];	// Array of product structures
	int i;						// Counter

	for (i = 0; i < Products_Amount; i++)
	{
		// set all product descriptions to an empy string, and all part numbers to zero, and all costs to zero.
		cout << "Part Name: ";
		getline(cin, items[i].description);
		

		cout << "Part #: ";
		cin >> items[i].partNum;
		cin.ignore();

		cout << "item cost:$ ";
		cin >> items[i].cost;
		cin.ignore();

		cout << endl;
				
	}

	cout << "First item information:\n";
	cout << fixed << showpoint << setprecision(2);
	
	{
		 cout << "Item 1: " << i << " " << items[i].description << endl;
	}

	return 0;
}
Last edited on
Remember that arrays start at zero and stop at size - 1.

that's understood, however do you mean that in terms of the increment operation or are you referring to putting an array number in the program to display only the intended array reference?

Well I got it to work just by messing around with the information.

Just providing in case someone ever came with a similar issue.

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

using namespace std;

struct Product
{
	string description;	// product description
	int partNum;	// Part Number
	double cost;	// Product cost
};


int main()
{
	const int Products_Amount = 2;	// array size and number of products
	Product items[Products_Amount];	// Array of product structures
	int i;						// Counter

	// Get product data
	cout << "Enter the product information for " << Products_Amount
		<< " products.\n";
		
	for (i = 0; i < Products_Amount; i++)
	{
		// get product information
		cout << "Part " << (i + 1);
		cout << " Name:";
		getline(cin, items[i].description);
		

		cout << "Part " << (i + 1);
		cout << " #:";
		cin >> items[i].partNum;
		cin.ignore();
		
		cout << "Part " << (i + 1);
		cout << " Cost: $";
		cin >> items[i].cost;
		cin.ignore();

		cout << endl;
				
	}

	cout << "First item Results:\n";
	cout << fixed << showpoint << setprecision(2);
	
	for (i = 0; i < Products_Amount; i++)
	{
		 cout << "Item 1 Information:\n";
		 cout << items[i].description << endl;
		  cout << items[i].partNum << endl;
		   cout << items[i].cost << endl;
		   break;
	}

	return 0;
}
Last edited on
Well done.
Topic archived. No new replies allowed.