I'm getting an error when I try to compile my code

I'm writing a command line based C++ program which will add some details to a struct array and which will retrieve them needed I'm only facing one compiler error which I don't know how to solve below is my full code.

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// e.cpp : Defines the entry point for the console application.


/********************************************************************************

Crated by Sadaruwan Samaraweera
In behalf of New Eastern Campus
Coding Start Date 22nd April 2013

*********************************************************************************/

#include<iostream>

#include<string>

 

using namespace std;

 
//Struct Declaration for stdDetails
struct stdDetails {

   	int stdNum; //Student Registration Number
    char fName[25]; // Student First Name
    char lName[25]; // Student Last Name
	string resAdd; //Resident Address
	char dBirth[10]; // Student Date Of Birth
	int stdAge; // Student Age
	string nicNum; // Sudent National ID number
	int conNum; // Student Contact Number
    char dateEnroll[10]; // Student Enrollment date
	char corsSelect[25]; // Course Selected
	char payMethod; // Fee payment method
    double amtPaid; // Amount Student Paid for the course    
    char bksIssue[25]; //Issued Books

};


//Declartion of methods usend in this program
void ui(); //Method for displaying the UI
void output(stdDetails  stdDetailsStructs_1);
stdDetails  enterDetails (stdDetails stdDetailsStructs_1); //Method for entering student details to the prgoram
void entLine(); //To make interface more nice
void stars(); //Draws a line of stars to make CLI look more nice

//Declaration of i as a Variable for the counter
int i=0;

//Start of main method
int main(){

	ui();
    return 0;
}

//CLI method start from here on
void ui (){

    int  choice;
    stdDetails  stdDetailsStructs_1;
	stdDetails  stdDetailsStructs_2[1000];
	//char ansr = 'y';
	char ansr;
	int search = 0;

	stars();
	cout<<"		Welcome to New Eastern Campus "<<endl;
	cout<<endl;
	stars();
	entLine();

//Beginning of the while loop which will display the options that user have to enter
		do{

			cout<<"		Press Number [1] for New Student Registration  : "<<endl;
			cout<<"		Press Number [2] to Search Student Details : "<<endl;
			cout<<"		Press Number [3] to Delete Student Details : "<<endl;
			cout<<"		Press Number [4] to View Student Details: "<<endl;
			entLine();
			cout<<"		Enter your choice here: ";
			cin>>choice;
			entLine();
//Start of the switch case for every choice selected will perform an action
			switch(choice){

//This Case will add the details entered to the stdDetails array
				case 1:
					stdDetailsStructs_1 = enterDetails(stdDetailsStructs_1);
					stdDetailsStructs_2[i]=stdDetailsStructs_1;
					i++;
				break;

//This Case will search the array for a given keyword 
				case 2:{
					stdDetails stdDetailsStructs_0;
					int kWord;
					stdDetails  stdFindRes;
					cout<<"Enter the Student Registration Number of the Student: ";
					cin>>kWord;
						for(int x=0;x<i;x++){
						stdFindRes = stdDetailsStructs_0[x];
						if(kWord == stdFindRes.stdNum){
								search=1;
							break;
							}
						}
						if(search==1){
							output(stdFindRes);
						}else{
							cout<<"Student details not found please try again."<<endl;
							search = 0;
						}
					   }
				break;

//This case will delete the students details form the array 
				case 3:{
					search=0;
					int delStd,m;
					stdDetails  stdDel;
					cout<<"Enter the Registration Numeber of the student: ";
					cin>>delStd;
						for(m=0;m<i;m++){
						stdDel = stdDetailsStructs_2[m];
							if(delStd==stdDel.stdNum){
								search=1;
							break;
							}
						}
						if(search==1){
							for(int z=m;z<i;z++){
								stdDetailsStructs_2[z]=stdDetailsStructs_2[z+1];
								i--;
							}
						}else{
							cout<<"Std  not found"<<endl;
						}
					   }
				break;
//This case will display the student details
				case 4:
					cout<<"=================================="<<endl<<endl;;
					for(int x=0;x<i;x++){
						stdDetails  stdView = stdDetailsStructs_2[x];
						output(stdView);
						cout<<endl;
						cout<<"***********************************"<<endl;
						cout<<endl;
					}
					cout<<"=================================="<<endl;
				break;

//The default message if  any thing goes wrong
				default:
					cout<<"Invalid Option Please Enter a Valid Entry"<<endl;
					break;
				}

//The option case list ends from here
				entLine();
				cout<<"Continue [y/n]: ";
				cin>>ansr;
				entLine();
			}while(ansr == 'y'||ansr == 'Y');

//While Loop Ends here

			cout<<"Thank You"<<endl;
}
//Ending of the UI system

//This method is used to decorate the interface
void entLine(){
	cout<<endl;
    cout<<endl;
}
//Method End

//Data Display Method Starts from here
void output(stdDetails  stdDetailsStructs_1){
    cout<<"Student Registration Number: "<<stdDetailsStructs_1.stdNum<<endl;
    cout<<"Student First Name: "<<stdDetailsStructs_1.fName<<endl;
    cout<<"Student Last Name: "<<stdDetailsStructs_1.lName<<endl;
	cout<<"Student Resident Address: "<<stdDetailsStructs_1.resAdd<<endl;
	cout<<"Student Date Of Birth: "<<stdDetailsStructs_1.dBirth<<endl;
	cout<<"Student Age: "<<stdDetailsStructs_1.stdAge<<endl;
	cout<<"Student National ID number: "<<stdDetailsStructs_1.nicNum<<endl;
	cout<<"Student Contact Number: "<<stdDetailsStructs_1.conNum<<endl;
	cout<<"Student Enrollment Date: "<<stdDetailsStructs_1.dateEnroll<<endl;
	cout<<"Selected Course: "<<stdDetailsStructs_1.corsSelect<<endl;
	cout<<"Payment Mthod: "<<stdDetailsStructs_1.payMethod<<endl;
	cout<<"Paid Amount: "<<stdDetailsStructs_1.amtPaid<<endl;
	cout<<"Books Issued: "<<stdDetailsStructs_1.bksIssue<<endl;
}
//Data Display Method Ends

//Data Entery Method start from here
stdDetails  enterDetails (stdDetails  stdDetailsStructs_1){

	cout<<"Enter Student Registration Number : ";
	cin>>stdDetailsStructs_1.stdNum;
	cout<<"Enter Sutdent Firs Name : ";
	cin>>stdDetailsStructs_1.fName;
	cout<<"Enter Student Last Name : ";
	cin>>stdDetailsStructs_1.lName;
	cout<<"Enter Student Resident Address : ";
	cin>>stdDetailsStructs_1.resAdd;
	cout<<"Enter Student DoB : ";
	cin>>stdDetailsStructs_1.dBirth;
	cout<<"Enter Student Age : ";
	cin>>stdDetailsStructs_1.stdAge;
	cout<<"Enter Student NIC number : ";
	cin>>stdDetailsStructs_1.nicNum;
	cout<<"Enter Student Contact number : ";
	cin>>stdDetailsStructs_1.conNum;
	cout<<"Enter Student Enrollment Date : ";
	cin>>stdDetailsStructs_1.dateEnroll;
	cout<<"Selected Course : ";
	cin>>stdDetailsStructs_1.corsSelect;
	cout<<"Payment Method : ";
	cin>>stdDetailsStructs_1.payMethod;
	cout<<"Amount Paid : ";
	cin>>stdDetailsStructs_1.amtPaid;
	cout<<"Books Issued : ";
	cin>>stdDetailsStructs_1.bksIssue;
	return stdDetailsStructs_1;
}
//Data Entry  Method End

//CLI Line of stars
void stars(){
for (int i=1; i<=50; i++) {
    cout << "*";
}
}


This is the compiler error I'm getting

e:\users\sinux\dropbox\assigenments\pp assignment\program\vs2012\stdreg2\stdreg2\source.cpp(103): error C2676: binary '[' : 'stdDetails' does not define this operator or a conversion to a type acceptable to the predefined operator

This error is generated due to this part of the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//This Case will search the array for a given keyword 
				case 2:{
					stdDetails stdDetailsStructs_0;
					int kWord;
					stdDetails  stdFindRes;
					cout<<"Enter the Student Registration Number of the Student: ";
					cin>>kWord;
						for(int x=0;x<i;x++){
						stdFindRes = stdDetailsStructs_0[x];
						if(kWord == stdFindRes.stdNum){
								search=1;
							break;
							}
						}
						if(search==1){
							output(stdFindRes);
						}else{
							cout<<"Student details not found please try again."<<endl;
							search = 0;
						}
					   }
				break;


Can some please help me to solve this this the only issue I have if this get solved I'm all good.
Last edited on
You're treating a single instance of your stdDetails class as if it's an array.

This line is causing the issue.
 
stdFindRes = stdDetailsStructs_0[x];

Thank you for your quick reply I'm fairly new to C++ programing can you help me to fix this please I searched all over the net to find a solution but got nothing in return.
You need to remove the [x] part of that line.

You've declared stdDetailsStructs_0 as a single instance of a stdDetails object. As it's a single instance, you can't use the [] operator to access it - it isn't an array.

This will fix your compilation issue. Whether it offers the functionality you want is another matter. :-)
So if I use it like this will it work

1
2
3
4
for(int x=0;x<i;x++){
						stdFindRes = stdDetailsStructs_0;
						if(kWord == stdFindRes.stdNum){
								search=1;


or is it like this

1
2
3
4
for(int x=0;x<i;x++){
						stdFindRes = stdDetailsStructs_0[];
						if(kWord == stdFindRes.stdNum){
								search=1;
which is correct I'm very sorry to be like this I'm still starting C++
Well, the question is - what do you want stdDetailsStructs_0 to be? Do you want it to be a single structure? Or do you want it to be an array of structures? What's it supposed to be for?

I notice that at line 103, you're trying to use it without ever having initialised it. That won't end well.
I got it to work the new code is below and yes I want it be searching data one by one in an array of data stored in struct array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
case 2:{
					stdDetails stdDetailsStructs_0[1000];
					int kWord;
					stdDetails  stdFindRes;
					cout<<"Enter the Student Registration Number of the Student: ";
					cin>>kWord;
						for(int x=0;x<i;x++){
						stdFindRes = stdDetailsStructs_0[x];
						if(kWord == stdFindRes.stdNum){
								search=1;
							break;
							}
						}
						if(search==1){
							output(stdFindRes);
						}else{
							cout<<"Student details not found please try again."<<endl;
							search = 0;
						}
					   }
				break;
I want it be searching data one by one in an array of data stored in struct array.

But there's nothing in the array to search. You're creating an array with:
 
stdDetails stdDetailsStructs_0[1000];


but you're not setting the values of any of the members of the array. The contents of that array are undefined. So when you copy an element of the array into a new structure with
 
stdFindRes = stdDetailsStructs_0[x];

the contents of stdFindRes will also be undefined.
Ok, I see so how can I correct this I'm mainly a PHP person C++ I just got started that's why I'm being such a noob please show me how to make this right
Well, at risk of stating the obvious, you need to actually set the values of the contents of that array. The thing is, I don't know what that array is supposed to contain - you do.

Did you really intend to create a new array here, and populate it? Or is this supposed to be searching an array that's been populated somewhere else in the code?
Topic archived. No new replies allowed.