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);
}

Recommended Answers

All 6 Replies

Please try to get the problem to occur not in 350 lines, but instead something manageable like 20 lines.

Dave

Also: please provide the exact input and output. As far as I can see (quickly, not carefully) there is no possible 'No added students' output.

When i start program:
press 1 for add student.
first you add Name , after you add fn...after press 5 for a list of students...enter a FN and you`ll see "No added students"..
edit:
I post all source because i have no idea where can be the problem..sorry about this

This is at least confusing and at worst wrong:

CStudent::CStudent(string name, string fn)
{
	name = name; // are you sure this works?
	fn = fn;     // are you sure this works?
}

I like to use this->name = name to absolutely disambiguate. I also like to pass parameters by const reference for several reasons (shown later):

CStudent::CStudent(const string& name, const string& fn)
{
	this->name = name; // this is guaranteed to work
	this->fn = fn;     // this is guaranteed to work
}

Reasons to pass parameters by const reference:

  1. Passing references does not invoke a copy constructor, which can be expensive (does not help for 'small' parameters such as int, nor for pointers)
  2. Passing by const reference assures that nothing 'leaks out of the function' via a non-const reference.
  3. Non-const data can be passed to such a function. If the function were to take non-const parameters, then const data could not be passed to it.
  4. note that there are also reasons not to pass by const reference, even for 'large' types, but my default is to use const reference: Usually best, sometimes just as good, and occasionally not so good.

You said:

... first you add Name , after you add fn...after press 5 for a list of students...enter a FN and you`ll see "No added students"..

The code shown here does not do that. If you press 5 using this code, you are asked to enter FN, and that particular student is looked up. If that student is not in the list, you'll see "Student not found".
What this tells me is that you are one or more of:

  • Confused about the relationship between your code and your executable
  • Using someone else's compiled program and showing us your code
  • Using your compiled program and showing someone else's code
  • Disorganized / chaotic about how you work on your problems
  • Too tired to maintain coherent thought

You can do something about these issues:

  • Always work in a subdirectory that is named for this current problem (or problem set)
  • Always use a makefile (or your IDE) in a consistent manner so as to have a current compiled program every time you run it.
  • Never share code with your classmates unless you are on the same team
  • Use some form of source control. Using CVS when I was in school saved me dozens of hours over three years. Maybe even hundreds.
  • And get a little more sleep, even if it means skipping something pleasant. Caffeine is better than nothing, but much inferior to sleep.
commented: Excellent +12

This is at least confusing and at worst wrong:

CStudent::CStudent(string name, string fn)
{
	name = name; // are you sure this works?
	fn = fn;     // are you sure this works?
}

I like to use this->name = name to absolutely disambiguate. I also like to pass parameters by const reference for several reasons (shown later):

CStudent::CStudent(const string& name, const string& fn)
{
	this->name = name; // this is guaranteed to work
	this->fn = fn;     // this is guaranteed to work
}

Reasons to pass parameters by const reference:

  1. Passing references does not invoke a copy constructor, which can be expensive (does not help for 'small' parameters such as int, nor for pointers)
  2. Passing by const reference assures that nothing 'leaks out of the function' via a non-const reference.
  3. Non-const data can be passed to such a function. If the function were to take non-const parameters, then const data could not be passed to it.
  4. note that there are also reasons not to pass by const reference, even for 'large' types, but my default is to use const reference: Usually best, sometimes just as good, and occasionally not so good.

Thanks.I forget about "this->" ...it`s still not working on Visual Studio 6 but it`s work perfect on Dev-C++ now ...
Thank you again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.