create the class Student h which will have the following characteristics:
● AM: (int)
● Name: (char *)
● Semester: (int)
● Active?: (bool)
Write a function that creates and returns a collection of objects of the Students class with data that are read from a csv file. The name of the file is given as an argument to the function.
Write a function that will print AM and the Name of each active student that is in her collection previous function. Write a main function that will display the operation of the above. THE Student class will not have public attributes and will only have the necessary ones methods for implementing the above requested
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Student {
private:
int AM;
string Name;
int Semester;
bool Active;
public:
// Constructor
Student(int am,string name, int semester, bool active)
{
AM = am;
Name = name;
Semester = semester;
Active = active;
}
Student()
{
AM = 0;
Name = "";
Semester = 0;
Active = false;
}
// Getter for AM
int getAM() {
return AM;
}
// Getter for Name
string getName() {
return Name;
}
// Getter for Semester
int getSemester() {
return Semester;
}
// Getter for Active
bool isActive() {
return Active;
}
};
// Function to read students from a CSV file
vector<Student> readStudentsFromFile(string filename) {
vector<Student> students;
// ifstream file object is used to access the file
ifstream file;
file.open(filename);
// If the file fails to open for some reason (e.g. the file doesn't exist),
// the fail member function of the ifstream file object will return true.
// If it does, we exit the function with an empty vector.
if (file.fail()) {
// Output an error message
cout << "File failed to open." << endl;
return students;
}
Student line;
while (!file.eof())
{
getline(file, line);
students.push_back(line);
}
// Close the file as we are now done working with it
file.close();
return students;
}
// Function to print active students
void printActiveStudents(vector<Student> students) {
for (int i = 0; i < students.size(); ++i) {
if (students[i].isActive()) {
cout << "AM: " << students[i].getAM() << ", Name: " << students[i].getName() << endl;
}
}
}
int main() {
// Create a collection of Student objects by reading from a CSV file
vector<Student> studentCollection = readStudentsFromFile("cs.csv");
// Print AM and Name of each active student
printActiveStudents(studentCollection);
return 0;
}
I cannot get this to work I need to get the lines from a file and do what I am asked. Issues in my code it says "message": "no instance of overloaded function "getline" matches the argument list". I do not know how to get this fixed I stuck for hours and tried different things around the code.
This is always a bug, in C and C++.
Your C++ textbook or instructor must've already discussed proper ways for handling and detecting end of file conditions when reading from files. A file stream's end-of-file condition gets set after an attempt to read from the file fails. This code attempts to check for an end of file condition before attempting to read the next record from the file. As you know, the
whileloop's condition is checked before executing the contents of the loop. But that's just the beginning of the problems here, most of the problem is here:The second parameter to
std::getlineis astd::string. Just because this variable's name is "line" doesn't automatically turn it into astd::string, C++ does not work this way. You declared thislineas aStudentobject (default constructed) here:This is what this line does, it declares a
Studentcalledline. So it is aStudent.std::getlinedoesn't know anything about yourStudentobject, all it knows how to do is read a line of text into astd::string.Therefore, you have no options but to read each line into a
std::string, first:This solves both problems: now each line gets read into a
std::string, andeof()gets checked after the attempt to read from the file, so if it fails the loop terminates, as expected.So now, you have a single
std::string, containing the entire line. You described the contents of the file as containing comma-separated values.You must implement the logic for extracting and parsing a line of comma-separated values by yourself, in your own code. There's nothing in the C++ library to do it for you. It is not clear from your question whether your textbook or your C++ instructor expects you to use some particular CSV-parsing library, or the entire purpose of your homework assignment is to implement the CSV-parsing logic by yourself, using iterators and/or basic algorithms from the C++ library (this is probably the likely situation).
But whatever the case may be, which only you know for sure (we don't know, on Stackoverflow, the full details of your assignment), you'll need to proceed according to what you've already read or been taught, by your C++ textbook or instructor, and extract the
Student's four required values, appropriately, and once they're extracted, pass them toStudent's constructor:Once you've implemented the appropriate logic, you'll use this constructor to, finally, create your
Studentobject and do whatever needs to be done with it.