I'm getting one error in my code

In the ReadStudents function for "Student Student { _id, c, _fname, _lname, _cname, _Icount };" I'm getting an error for the curly brackets.


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

enum class gender {
Male, Female
};

class Student {
private:
string id, fname, lname, cname;
int Icount;
gender g;
public:
Student() { fname = "None"; lname = "None"; id = "None"; cname = "None"; Icount = 0; g = gender::Female; }
Student(string _id, gender _g, string _fname, string _lname, string _cname, int _Icount) {
fname = _fname;
lname = _lname;
id = _id;
cname = _cname;
Icount = _Icount;
g = _g;
}
void setfname(string _fname) { fname = _fname; } string getfname() {
return fname;
}
void setlname(string _lname) { fname = _lname; } string getlname() {
return lname;
} void setid(string _id) { fname = _id; } string getid() {
return id;
} void setcname(string _cname) { fname = _cname; } string getcname() {
return cname;
}
void setIcount(int _Icount) { Icount = _Icount; } int getIcount() {
return Icount;
}
void setg(gender _g) { g = _g; } gender getg() {
return g;
}
void print() {
int c;
if (c == 1) cout << "Female"; else cout << "Male";
cout << "ID [" << id << "] First Name [" << fname << "] Last Name [" << lname << "] Gender [" << c << "] Course [" << cname << "] Attendance [" << Icount << "]\n";
}
};

vector<Student> readStudents(string filename) {
int size;
string temp;
vector <Student> students;
ifstream f;
f.open("Text.txt");
if (f.fail()) {
cout << "Failed to open file"; exit(1);
}
f >> size; getline(f, temp); getline(f, temp);
for (int i = 0; i < size; i++) {
int c; string _fname, _lname, _id, _cname; gender _g; int _Icount;
f >> _id >> c >> _fname >> _lname >> _cname >> _Icount;
_g = gender::Female; if (c == 0)_g = gender::Male; else _g = gender::Female;
Student Student { _id, c, _fname, _lname, _cname, _Icount };
students.push_back(Student);
}
return students;
}
void printStudents(vector<Student> stds)
{
string c;
for (int i = 0; i < stds.size(); i++) {
if (stds[i].getg() == gender::Female) c = "Female";
else c = "Male";
cout << "ID [" << stds[i].getid() << "] First Name [" << stds[i].getfname() << "] Last Name [" << stds[i].getlname() << "] Gender [" << c << "] Course [" << stds[i].getcname() << "] Attendance [" << stds[i].getIcount() << "]\n";

}
}

vector<Student> getFemales(const vector<Student>& stds) {
int i = 0;
vector <Student> stf = stds;
while (stf[i].getg()== gender::Female)
{
printStudents(stf); i++;
}
}

vector<Student> getLowAttendance(const vector<Student>& stds) {
int i = 0;
vector <Student> stl = stds;
while (stl[i].getIcount() < 20) {
printStudents(stl); i++;
}
}


int main()
{

vector <Student> stds_vect;
string file;
stds_vect = readStudents(file);
for (int i = 0; i < stds_vect.size(); i++) {
printStudents(stds_vect);
}
getFemales(stds_vect);
getLowAttendance(stds_vect);


return 0;
Last edited on
When posting code, please use code tags so that the code is readable

[code]
// code goes here
[/code]



"Student Student { _id, c, _fname, _lname, _cname, _Icount };"

c should be _g.

There are other issues. As a first refactor, consider (not tried as no test data provided):

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

enum class gender { Male, Female };

class Student {
private:
	std::string id {"None"}, fname {"None"}, lname {"None"}, cname {"None"};
	int Icount {};
	gender g {gender::Female};

public:
	Student() {}
	Student(const std::string& _id, gender _g, const std::string& _fname, const std::string& _lname, const std::string& _cname, int _Icount) :
		fname(_fname), lname(_lname), id(_id), cname(_cname), Icount(_Icount), g(_g) {}

	void setfname(const std::string& _fname) { fname = _fname; }
	std::string getfname() const { return fname; }
	void setlname(const std::string& _lname) { fname = _lname; }
	std::string getlname() const { return lname; }
	void setid(const std::string& _id) { fname = _id; }
	std::string getid() const { return id; }
	void setcname(const std::string& _cname) { fname = _cname; }
	std::string getcname() const { return cname; }
	void setIcount(int _Icount) { Icount = _Icount; }
	int getIcount() const { return Icount; }
	void setg(gender _g) { g = _g; }
	gender getg() const { return g; }

	void print() const {
		const std::string gen {g == gender::Female ? "Female" : "Male"};

		std::cout << "ID [" << id << "] First Name [" << fname << "] Last Name [" << lname << "] Gender [" << gen << "] Course [" << cname << "] Attendance [" << Icount << "]\n";
	}
};

std::vector<Student> readStudents(const std::string& filename) {
	std::ifstream f(filename);

	if (!f) {
		std::cout << "Failed to open file";
		exit(1);
	}

	size_t size {};
	std::string temp;
	std::vector <Student> students;

	f >> size;
	std::getline(f, temp);
	std::getline(f, temp);

	for (size_t i = 0; i < size; ++i) {
		int c {}, _Icount {};
		std::string _fname, _lname, _id, _cname;

		f >> _id >> c >> _fname >> _lname >> _cname >> _Icount;

		students.emplace_back(_id, c== 0 ? gender::Male : gender::Female, _fname, _lname, _cname, _Icount);
	}
	return students;
}

void printStudents(const std::vector<Student>& stds) {
	for (const auto& s : stds)
		s.print();
}

std::vector<Student> getFemales(const std::vector<Student>& stds) {
	std::vector<Student> stf;

	for (const auto& s : stds)
		if (s.getg() == gender::Female) {
			s.print();
			stf.push_back(s);
		}

	return stf;
}

std::vector<Student> getLowAttendance(const std::vector<Student>& stds) {
	std::vector<Student> stla;

	for (const auto& s : stds)
		if (s.getIcount() < 20) {
			s.print();
			stla.push_back(s);
	}

	return stla;
}

int main() {
	std::vector <Student> stds_vect {readStudents("Text.txt")};

	printStudents(stds_vect);
	getFemales(stds_vect);
	getLowAttendance(stds_vect);
}

Last edited on
Topic archived. No new replies allowed.