This is my code:
class Student {
const std::string& Name;
const std::string& Pinfo;
std::map < std::string, float > Ngradelist;
public:
Student(const std::string& n, const std::string& p, std::map < std::string, float > Ng) :
Name(n), Pinfo(p), Ngradelist(std::move(Ng)) {}
virtual void Display() {
std::cout << "Name: " << Name << std::endl;
std::cout << "Personal information: " << Pinfo << std::endl;
std::cout << "NCourse:" << std::endl;
for(const auto& it : Ngradelist)
std::cout << " " << it.first << " Grade: " << it.second << std::endl;
}
};
void InsertListaStudenti(Student& student, std::list < Student > &listaStudenti) {
listaStudenti.push_front(student);
}
int main () {
std::list < Student > listaStudenti;
unsigned short option = 10;
while(option != 0) {
std::cout << "1.Add student." << std::endl;
std::cout << "4.Display Student." << std::endl;
std::cout << "0 is for exit." << std::endl;
std::cout << "Option is : ";
std::cin >> option;
switch (option) {
case 0 : {
std::cout <<" You chose to leave.";
exit;
break;
}
case 1 : {
std::string name , pinfo, course;
float grade = 0;
std::cout << "Insert name : ";
std::cin >> name;
std::cout << "Insert personal information : ";
std::cin >> pinfo;
std::cout << "Insert course : ";
std::cin >> course;
std::cout << "Insert grade : ";
std::cin >> grade;
Student student(name, pinfo, );
// listaStudenti.push_back(student);
InsertListaStudenti(student,listaStudenti);
break;
}
case 4: {
for(auto &it : listaStudenti)
it.Display();
break;
}
}
}
The code works fine without using a list where I store these datas. When I add one student, there is no problem, but when i add the second student, the Name and PInfo from the first student will change to the Name and Pinfo of the second student. I'll give an example so you can understand maybe better. Student 1 has these datas: Name=a, Pinfo=a, Course=a, grade=1. Student 2 has these datas: Name=b, Pinfo=b, Course=b, grade=2. If i want to display, it would be like this: student 1 will have b b a 1 and student 2 will have b b b 2
Does anyone know what I can do to make this work ? I believe that those 2 fields( name and pinfo) are overriden everytime I create a new object in the main,but I'm not sure about it..
Aucun commentaire:
Enregistrer un commentaire