After compile 2 error shows

I'm beginner of c++, no knowledge on c++. I'm writing a code for student and module. The requirement is need to displayed the module and the student with final score, also the duplication. When I write the case 5 and case 6, after compile it show the error below.


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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include "Student.h"
#include "Module.h"
using namespace std;

void printMenu();
vector<Student> readFile();
void liststudentRecord(vector<Student>);
string trim(string);
bool isValidClasscode(string s);
bool isvalidStudentid(string s);
bool test1Ascending(Student s1, Student s2);
bool test1Desscending(Student s1, Student s2);
bool test1finalScoreAscending(Student s1, Student s2);
vector<Module> readFile2();
void listmoduleRecord(vector<Module>);
bool moduleName (Module m1, Module m2);
vector<Student> getStudentByModule(vector<Student>,string);
bool isDuplicateStudentRecord(vector<Student>, Student);


//gloval variable
vector<Student> duplicates;

int main() {

	int choice;
	vector<Student> students;//list of student
	vector<Module> modules;
	vector<Student> list;

	do {
		printMenu();
		cin >> choice;

		switch(choice) {
		case 1: 
			cout << "Read Student file ... " << endl; 
			students =readFile();
			cout<<"Number of records read: " <<students.size() <<endl;
			break;
		case 2:
			 liststudentRecord(students);
			 break;
		case 3: 
			cout << "Read Module file ... " << endl; 
			modules =readFile2();
			cout<<"Number of records read: " <<modules.size() <<endl;
			break;
		case 4:
			listmoduleRecord(modules);
			break;
		
		case 5:
			liststudentRecord(duplicates);
			break;
		case 6:
			cout << "Enter Module Code: ";
			string code;
			cin >> code;
			list = getStudentByModule(students, code);
			for (int i=0; i < list.size(); i++ ){
				Student s = list.at(i);
				cout << s.toString() << endl;
			}
			cout<<"List Student by Module Code"<<endl;
			break;
		case 7:
			//http://www.cplusplus.com/reference/algorithm/sort/
			//sort(students.begin(), students.end(), test1finalScoreAscending);
			sort(students.begin(), students.end(),test1finalScoreAscending); //finalScoreAcending
			break;
		case 8:
			break;
		case 9: cout << "Exiting program... " << endl; break;
		default: 
			cout << "Invalid option specified. Please try again" << endl;
		}
	} while(choice != 9);

	return 0;
}

void printMenu() {
	cout << "Menu" << endl;
	cout << "----" << endl;
	cout << "[1] Read student file" << endl;
	cout << "[2] List student records" << endl;
	cout << "[3] Read module file" << endl;
	cout << "[4] List module records" << endl;
	cout << "[5] List Duplicate Record" << endl;
	cout << "[6] Display Student by Module" << endl;
	cout << "[7] Sort by Final Score" <<endl;
	cout << "[8] Write to file" << endl;
	cout << "[9] Exit" << endl;
	cout << "Choice: ";
}

vector<Student>readFile(){
	vector<Student>students;
	string filename;
	ifstream inputFile;

	cout << "Enter filename: ";
	cin >> filename;

	inputFile.open(filename, ios::in);//open file, read only
	if(inputFile.is_open()){
		while (inputFile.good()){
			string line;
			getline(inputFile, line);

			Student s; //create a new student

			//extract id
			int i = line.find(",");
			string id = trim(line.substr(0, i));// extract id
			s.setId(id); // set student id
			line = line.substr(i+1);// remove id form line
			

			//extract name
			i = line.find(","); 
			string name = trim(line.substr(0, i)); // extract name
			s.setName(name); //set student name
			line = line.substr(i+1); //remove name from line

			//extract classcode
			i = line.find(",");
			string classcode = trim(line.substr(0, i));
			s.setClasscode(classcode);
			line = line.substr(i+1);
			if(isValidClasscode(classcode)){
				string module = classcode.substr(0,6);
				s.setModule(module);
				string status =  classcode.substr(7,9);
				char *ft ="FT";
				if(status.compare(ft)==0){
					s.setFulltime(true);
				}else{
					s.setFulltime(false);
				}
				string classnumber = classcode.substr(9,11);
				s.setClassnumber(classnumber);
			}
			

			//to use a for-loop for attendance
			for (int j =0; j < 10 ; j++){
				i = line.find(",");
				string attendance = trim(line.substr(0, i));
				s.setAttendance(j, attendance);
				line = line.substr(i+1);
			}

			//to extreact test1
			i = line.find(",");
			int test1 = stoi(line.substr(0, i));
			s.setTest1(test1);
			line = line.substr(i+1);

			//to extreact test2
			i = line.find(",");
			int test2 = stoi(line.substr(0, i));
			s.setTest2(test2);
			line = line.substr(i+1);


			//to extreact tutorial
			i = line.find(",");
			int tutorial = stoi(line.substr(0, i));
			s.setTutorial(tutorial);
			line = line.substr(i+1);

			//to extreact exam
			i = line.find(",");
			int exam = stoi(line.substr(0, i));
			s.setExam(exam);
			line = line.substr(i+1);


			//need to do validationfirst
			//to only add the valid student

			if(isDuplicateStudentRecord(students, s) == false){
			students.push_back(s); //add student to list
			} else {
				duplicates.push_back(s);
			}
			//cout<< line << endl;
		}
		inputFile.close();
	}else{
		cout << "Invalid file" << endl;
	}

	return students;
}

void liststudentRecord(vector<Student> list){
	int numberStudents = list.size();

	if (numberStudents > 0){
		for (int i = 0; i < numberStudents; i++){
			Student s = list.at(i);
			cout << s.toString() << endl;
		}
	}else{
			cout << "Empty list" <<endl;
			
		}
}


bool finalScoreAscending(Student s1, Student s2){
	return s1.getFinalScore() < s2.getFinalScore();
}





vector<Student> getStudentByModule(vector<Student> students,string code){
	vector<Student> list;
	for (int i=0; i< students.size(); i++){
		Student s = students.at(i);
		if (code.compare(current_module) == 0){
			list.push_back(s);
		}
	}
	return list;
}


bool isDuplicateStudentRecord(vector<Student> student, Student s){
	string id = student.getId();
	string name = student.getName();
	string module = student.getModule();
	for(int i=0; i< student.size(); i++){
		Student  s = student.at(i);
		string current_id = s.getId();
		string current_name =  s.getName();
		string current_module = s.getModule();
		if(id.compare)(current_id) == 0 &&
			name.compare(current_name) == 0 &&
			(module.compare(current_module) == 0);{
			return true;
		}
	}
	return false;
}


//http://stackoverflow.com/a/6500499/3839235
string trim(string s){
	s.erase(0, s.find_first_not_of(' ')); //prefixing spaces
	s.erase(s.find_last_not_of(' ')+1); //surfixing spaces
	return s;
}


Please help to solve error code for duplication and the displayed of module and student.

error C2360: initialization of 'code' is skipped by 'case' label
see declaration of 'code'
error C2361: initialization of 'code' is skipped by 'default' label
see declaration of 'code'
error C2039: 'getId' : is not a member of 'std::vector<_Ty>'
1> with
1> [
1> _Ty=Student
1> ]

'getModule' : is not a member of 'std::vector<_Ty>'
1> with
1> [
1> _Ty=Student
1> ]

Thank you.
Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
		case 6:
			cout << "Enter Module Code: ";
			string code;
			cin >> code;
			list = getStudentByModule(students, code);
			for (int i=0; i < list.size(); i++ ){
				Student s = list.at(i);
				cout << s.toString() << endl;
			}
			cout<<"List Student by Module Code"<<endl;
			break;


In this snippet you are trying to declare a variable inside the case statement. This is not allowed unless you use the optional braces to create a scoped block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

             case 6:
             {
			cout << "Enter Module Code: ";
			string code;
			cin >> code;
			list = getStudentByModule(students, code);
			for (int i=0; i < list.size(); i++ ){
				Student s = list.at(i);
				cout << s.toString() << endl;
			}
			cout<<"List Student by Module Code"<<endl;
			break;
            }


But remember that that variable will only exist within that block. Normally the variable would be declared before the the switch statement.


Thanks JLB,

You've solved one of my problem.

Now the other issue is.

partb.cpp(234): error C2065: 'current_module' : undeclared identifier
partb.cpp(243): error C2039: 'getId' : is not a member of 'std::vector<_Ty>'
1> with
1> [
1> _Ty=Student
1> ]
partb.cpp(244): error C2039: 'getName' : is not a member of 'std::vector<_Ty>'
1> with
1> [
1> _Ty=Student
1> ]
partb.cpp(245): error C2039: 'getModule' : is not a member of 'std::vector<_Ty>'
1> with
1> [
1> _Ty=Student
1> ]
C265 is because in the function
vector<Student> getStudentByModule(vector<Student> students,string code)
You used the variable current_module without declaring it.

The rest is because in isDuplicateStudentRecord, the argument student is of type std::vector, not of Student. You should do s.getId() or something like that in the begging of the function and use student[i] in your for loop.
Last edited on
Hi Tyler thanks for the reply. I did some changes on this code.

you may help me check where is the error?

http://melpon.org/wandbox/permlink/qLnE80Hs6FJWl9Fy

I'm really 0 knowledge in C++

Thank you.
Topic archived. No new replies allowed.