I'm having a problem on passing a struct array to a function. Help me please.

I'm now getting closer to finishing my program but I am having a problem in outputting my inventory. What's wrong with my code?
(Our instructor told us not to touch those functions that she have given.)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<iostream>
#include<string>
#include<cctype>
#include<cstdlib>
#include <iomanip>
#include <windows.h>
#include<conio.h>
int i, j;

using namespace std;

//////// function that takes the direction of x and y coordinates////////
void gotoxy( HANDLE StdOut, SHORT x, SHORT y )
{
	// Set the cursor position.
	COORD Cord;
	Cord.X = x;
	Cord.Y = y;
	SetConsoleCursorPosition( StdOut, Cord );
}
////////////use this structure name in creating the members of inventory//////////////
struct Products
{
	//add code here;
	string prod_name;
	unsigned int prod_size;
	string brand_name;
	double price;
	int stock;
	int sold;
	int left;
	Products *new_brand;
	
};


////////////////////////////////////////////////prototypes of functions
void password();
int ENTERPROD();
void INPUTPROD(int val);
void PDISPLAY(Products temp, int sval);


////////////////////////////////////////////////////////////////////

void main()   // DO NOT ADD or REVISE ANYTHING FROM THIS FUNCTION
{
	int count;

	password();

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);


	cout<<"		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl<<endl;

	count=ENTERPROD();

	cout<<endl<<"ENTER "<<count<<" PRODUCTS"<<endl;

	INPUTPROD(count);

	cout<<"\n\n\n\n\n\n\n";
	system("pause");
}//end main


////////////////////////////////////
int ENTERPROD()
{
	//add code here
	int size;
	cout << "ENTER NO. OF PRODUCTS FOR INVENTORY: ";
	cin >> size;
	
	return size;
}

/////////////////////////////////////
void INPUTPROD(int val)
{
	//add code here
	//call function PDISPLAY HERE…….
	Products *newProd;
	newProd = new Products[val];
	const int prod_size = 100;
	for (i = 0; i < val; ++i){
		cout << "Product[" << i+1 <<"]: ";
		cin >> newProd[i].prod_name;

		cout << "How many " << newProd[i].prod_name << "? ";
		cin >> newProd[i].prod_size;
		cout << endl;

		newProd[i].new_brand = new Products[newProd[i].prod_size];
		for(j = 0; j < newProd[i].prod_size; ++j){
			cout << newProd[i].prod_name << "[" << j+1 << "]: ";
			cin >> newProd[i].new_brand[j].brand_name;
			cout << "Price: ";
			cin >> newProd[i].new_brand[j].price;
			cout << "Stock: ";
			cin >> newProd[i].new_brand[j].stock;
			cout << "Sold: ";
			cin >> newProd[i].new_brand[j].sold;
			cout << endl;
			newProd[i].new_brand[j].left = newProd[i].new_brand[j].stock - newProd[i].new_brand[j].sold;
		}
		cout << endl;
	}
	PDISPLAY(*newProd, val);
}

//////////////////////////////////I AM HAVING A PROBLEM IN THIS PART
void PDISPLAY(Products temp, int sval)
{
	HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
	//add code here
	system("cls");
	cout << "		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl;
	cout << "PROD NO.   PRODUCT       NAME       PRICE    STOCK    SOLD   LEFT\n";
	for (i = 0; i < sval; i++){
		gotoxy(hStdout, 3, i+2);
		cout << i+1;
		gotoxy(hStdout, 11, i+2);
		cout << temp[i].prod_name; //THE COMPILER WON'T ACCEPT THIS ONE
	}
}
Last edited on
You state on line 116 that 'temp' is a Products object. Products does not override operator[], so there is no reason to try such operation.

Who did wrote the signature of PDISPLAY? What is the purpose of the parameter 'sval'?
Looks like you need modify line 116 to

 
PDISPLAY(newProd, val);


and modify the delaration of PDISPLAY to

 
void PDISPLAY(Products *temp, int sval)


val, and hence sval, appear to be the number of Products' in the array newProd.
Topic archived. No new replies allowed.