pls help me with this one..everything is ok but when i add student and faculty number and then try to list them says "No added students'...:@
This is:
main.cpp
#include <iostream>
#include "Student.h"
#include "Course.h"
using namespace std;
list<CStudent*> students;
list<CCourse*> courses;
CCourse* findCourse(string name)
{
CCourse* course = NULL;
//find course
for (list<CCourse*>::iterator i = courses.begin(); i != courses.end(); i++)
{
if ((*i)->GetName().compare(name) == 0)
{
course = *i;
}
}
return course;
}
CStudent* findStudent(string fn)
{
CStudent* student = NULL;
//find student
for (list<CStudent*>::iterator i = students.begin(); i != students.end(); i++)
{
if ((*i)->GetFN().compare(fn) == 0)
{
student = *i;
}
}
return student;
}
int main()
{
do
{
system("cls");
cout << "1. add student\n";
cout << "2. create course\n";
cout << "3. add student to course\n";
cout << "4. add scores for student\n";
cout << "5. display info for student\n";
cout << "6. display info for course\n";
cout << "0. Quit\n\n";
cout << "choice: ";
int answer = 0;
cin >> answer;
system("cls");
if (answer == 0)
{
break;
}
switch (answer)
{
case 1 :
{
cout << "add student\n";
string name, fn;
cout << "Name: ";
cin.ignore();
getline(cin, name);
cout << "FN: ";
getline(cin, fn);
students.push_back(new CStudent(name, fn));
cout << "1 student added!\n";
break;
}
case 2 :
{
cout << "create sourse\n";
string name;
cout << "Name: ";
cin.ignore();
getline(cin, name);
courses.push_back(new CCourse(name));
cout << "Course created!\n";
break;
}
case 3 :
{
cout << "add student to course\n";
string fn, courseName;
cout << "FN: ";
cin.ignore();
getline(cin, fn);
cout << "Course: ";
getline(cin, courseName);
CCourse* course = findCourse(courseName);
CStudent* student = findStudent(fn);
if (course == NULL)
{
cout << "course not found\n";
break;
}
if (student == NULL)
{
cout << "student not found\n";
break;
}
course->MakeStudent(student);
cout << "Student added to course!\n";
break;
}
case 4 :
{
cout << "add scores for student\n";
string fn;
cout << "FN: ";
cin.ignore();
getline(cin, fn);
CStudent* student = findStudent(fn);
if (student == NULL)
{
cout << "student not found\n";
break;
}
cout << "how many scores: ";
int n = 0;
cin >> n;
if (n > 0)
{
for (int i = 0; i < n; i++)
{
cout << "score: ";
int mark = 0;
cin >> mark;
if ((mark >= 2) && (mark <= 6))
{
student->AddScore(mark);
}
else
{
cout << "Invalid mark\n";
i--;
}
}
cout << n << " scores added\n";
}
break;
}
case 5 :
{
cout << "display info for student\n";
string fn;
cout << "FN: ";
cin.ignore();
getline(cin, fn);
CStudent* student = findStudent(fn);
if (student == NULL)
{
cout << "Student not found\n";
break;
}
cout << "Name: " << student->GetName() << "\n";
cout << "Average: " << student->GetGrade() << "\n";
break;
}
case 6 :
{
string courseName;
cout << "Course: ";
cin.ignore();
getline(cin, courseName);
CCourse* course = findCourse(courseName);
if (course == NULL)
{
cout << "course not found\n";
break;
}
cout << course->DisplayScores();
break;
}
}
system("pause");
}
while (true);
return 0;
}
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <list>
#include <string>
using namespace std;
class CStudent
{
public:
CStudent(string name, string fn); // constructor fn,name
string GetName(); // accessor za name
string GetFN(); // accessor za fn
double GetGrade(); // accessor za sredna ocenka
void AddScore(int grade); // dobavqne na ucenka
private:
string name; // ime
list<int> scores; // spisyk s ocenki
string fn; // fn
friend class CCourse;
};
#endif
Course.h
#ifndef COURSE_H_
#define COURSE_H_
#include <list>
#include <string>
#include <sstream>
#include "Student.h"
using namespace std;
class CCourse
{
public:
CCourse(string name); //Constructor name off course
string GetName(); // accessor za name
void AddScore(string fn, int grade); // add scores
void AddScore(string fn, list<int> grade); // add group of scores for student
string DisplayScores(); // display name,fn and average scores
void MakeStudent(CStudent* student); // add student with fn i ime kym kurs
private:
list<CStudent*> student_list; // list students
string name; // name of course
};
#endif /* COURSE_H_ */
Student.cpp
#include "Student.h"
CStudent::CStudent(string name, string fn)
{
name = name;
fn = fn;
}
string CStudent::GetName()
{
return name;
}
string CStudent::GetFN()
{
return fn;
}
double CStudent::GetGrade()
{
double avgGrade = 0.0;
if (scores.size() == 0)
{
return 0;
}
for (list<int>::iterator i = scores.begin(); i != scores.end(); i++)
{
avgGrade += *i;
}
return avgGrade / scores.size();
}
void CStudent::AddScore(int grade)
{
scores.push_back(grade);
}
Course.cpp
#include "Course.h"
CCourse::CCourse(string name)
{
this->name = name;
}
string CCourse::GetName()
{
return name;
}
void CCourse::AddScore(string fn, int grade)
{
for (list<CStudent*>::iterator student = student_list.begin(); student != student_list.end(); student++)
{
if ((*student)->fn.compare(fn) == 0)
{
(*student)->AddScore(grade);
}
}
}
void CCourse::AddScore(string fn, list<int> grade)
{
for (list<int>::iterator student_grade = grade.begin(); student_grade != grade.end(); student_grade++)
{
AddScore(fn, *student_grade);
}
}
string CCourse::DisplayScores()
{
stringstream ss;
ss << "=== " << name << "===\n";
for (list<CStudent*>::iterator student = student_list.begin(); student != student_list.end(); student++)
{
ss << "Name: " << (*student)->GetName() << "\n";
ss << "Marks: ";
for (list<int>::iterator student_grade = (*student)->scores.begin(); student_grade != (*student)->scores.end(); student_grade++)
{
ss << *student_grade << " ";
}
ss << "\nAverage: " << (*student)->GetGrade() << "\n\n";
}
return ss.str();
}
void CCourse::MakeStudent(CStudent* student)
{
student_list.push_back(student);
}