Structs as Arrays

I have a nested loop in my user-defined function INPUTPROD that terminates after the first run. I cant seem to understand what causes it to terminate. Help Pls. TIA

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
 #include<iostream>
#include<string>
#include<cctype>
#include<cstdlib>
#include <iomanip>
#include <windows.h>
#include<conio.h>
#include<ctype.h>

using namespace std;
////////////use this structure name in creating the members of inventory//////////////
struct Products
{
	//add code here;
	string productType;
	int prodQuantity;
	string prodName[];
	double prodPrice[];
	int prodStock[];
	int prodSold[];
};
//////////////////////////////code for getting the password//////////////////////////////////
string EnterPassword()
{
   string NumAsString="";
   char ch=getch();//h
   while (ch!='\r')
   {//true
	   
	   cout<<'*';//*****
	   NumAsString+=ch;//"hello"
	   ch=getch();//enter/return
	
		
   }

   return NumAsString;

}
/////////////////////prototypes of functions////////////////////////////////////////////////
void password();
int ENTERPROD();
void INPUTPROD(int val);
////////////////////////////////////////////////////////////////////

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

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

	cout<<"		***** INVENTORY SYSTEM CS127L 4TH QTR*****"<<endl<<endl;
    count=ENTERPROD();

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


	INPUTPROD(count);

	
	cout<<endl;
	system("pause");
}


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

/////////////////////////////////////
void INPUTPROD(int val)
{
    Products prodArray[val];
	int i,prodIndex,quantity;
	for(i=0;i<val;i++)
	{
			cout<<"Product"<<i+1<<":";
			getline(cin,prodArray[i].productType);
			cout<<"How many "<<prodArray[i].productType<<" ?";
			cin>>quantity;
			cin.ignore();
			for(prodIndex=0;prodIndex<quantity;prodIndex++)
			{
				cout<<prodArray[i].productType<<"["<<prodIndex+1<<"]: \n";
				getline(cin,prodArray[i].prodName[prodIndex]);
				cout<<"Price: \n";
				cin>>prodArray[i].prodPrice[prodIndex];
				cin.ignore();
				cout<<"Stock: \n";
				cin>>prodArray[i].prodStock[prodIndex];
				 cin.ignore();
				cout<<"Sold: \n";
				cin>>prodArray[i].prodSold[prodIndex];
				 cin.ignore();
			
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////
void password()
{
       //add code here
	//call function EnterPassword HEREā€¦	
	string userId,userPassword,pW,secretPw;
	userPassword="jaymark";
	secretPw="12345";
	int tryUser=3;
	int tryPw=3;
	cout<<"PRODUCT INVENTORY SYSTEM CS127L";
	
	bool again=true;
	while(again)
	{
	  	cout<<"\nYou have "<<tryUser<<" tries remaining";	
  		cout<<"\nUsername: ";
		getline(cin,userId);
	
		try
	 	{
	 		if (userPassword.compare(userId)!=0)
				{
				throw userId;
				break;
				}
			again=false;
		} 
	
	
		catch(string x)
		{
				tryUser--;
				if (tryUser==0)
				{
					
					cout<<"Maximum tries reached! Terminating!";
					exit(1);
				}		
				else
				{
					cout<<"Invalid Username!"<<"\nTry Again!\n";
				
				continue;
				}	
		}
	}
	cout<<"Welcome! "<<userId<<endl;
	Sleep(1000);
	system("cls");

	bool passAgain=true;
	while(passAgain)
	{
	  	cout<<"\nYou have "<<tryPw<<" tries remaining";	
  		cout<<"\nPassword: ";
		pW=EnterPassword();
		try
	 	{
	 		if (secretPw.compare(pW)!=0)
				{
				throw userId;
				break;
				}
			passAgain=false;
		} 
		catch(string y)
		{
				tryPw--;
				if (tryPw==0)
				{
					
					cout<<"Maximum tries reached! Terminating!";
					exit(1);
				}		
				else
				{
					cout<<"\n\nWrong Password!"<<"\nTry Again!";
				
				continue;
				}	
		}
	}
}

Lines 17-20: How many occurrences of the member variables do you think you're allocating? Hint: 0

Line 86: This is not standard C++. In standard compliant C++, array bounds must be known at compile time.

Line 86: prodArray is a local variable. It goes out of scope when INPUTPROD exits.

Line 98,100,103,106: Exactly which instance of a zero sized array do you think you're reading into?




Topic archived. No new replies allowed.