Ok, some notes on the code.
The following files:
Main.cpp (main program)
Fakultetas.* (Faculty class)
Studentas.* (Student class)
Duomenys.txt (data [read for input])
Rezultatai.txt (results [written as output])
The compile errors I get are:
"Studentas.h" line 11: You are initializing
np before firstname, lastname, and group. This is an issue because the compiler likes you to initialize things in the same order you declare them. Make the line read:
Studentas():pavarde(""), vardas(""), grupe(""), np(0){}
"Students.h" line 18: Since
operator!() is a member function, you don't need to put
Studentas:: in front of it. Make the line read:
bool operator!(){
"Main.cpp" line 63: You have a variable named
ns that you don't use. I'm not sure what it is there for, so unless you planned to use it somehow just delete that line.
You cannot
modify a student in the faculty class, and it appears that you are trying to do just that with your
Kurti() function. I'm not sure that I'm getting that translated properly (I get something along the lines of "create").
What exactly are you trying to do there? Remove failing students from the list, right?
But that is not what is happening.
The reason your program is breaking is that
R and
D are actually the
same faculty object. Every time you find a passing student, you add him to the end of the list of students. As a result, your loop will never find the end of the list, because you keep adding to it.
And after 100 students, you are modifying parts of your program that are not allowed, so it crashes.
You need to make sure that you are passing
different faculty members to the function.
1 2 3 4 5 6 7 8 9 10 11
|
int main(){
Fakultetas info; // duomenys // faculty info
Duomenys(info); // skaitomi duomenys // data( info )
Spausdinti(info, "Pradiniai duomenys"); // print( info, "the input data" )
Fakultetas gerus_pazymius; // the new list of students (with passing grades)
Kurti(info, gerus_pazymius); // create( info, info )
Spausdinti(gerus_pazymius, "Gerus Pazymius"); // print the students with passing grades
//system("pause");
return 0;
}
|
There are a couple other things you need to consider. I actually think you are misunderstanding some things about how your code should be structured.
I'll take them one at a time.
Let's look at the faculty class "Fakuletas.h":
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Fakultetas{ // class faculty
public:
static const int CMax = 100;
private:
Studentas St[CMax];
int n;
public:
Fakultetas():n(0) {}
int Imti() {return n;} // get()
Studentas Imti(int i){return St[i];} // get(i)
void Deti(const Studentas & ob) {St[n++] = ob;} // kas cia? // set(ob)... what is this?
string Spausdinti(); // print()
};
|
Line 11 appears to be designed to
add a student to the faculty member's list of students.
The problem is that it is not possible to either
modify a student or
remove a student. Once a student is added, it cannot be changed.
In "Fakultetas.cpp" you have two empty functions -- a constructor and a destructor, both do nothing. Get rid of them. You already have a default constructor on line 8 of "Fakultetas.h".
What you need to put in "Fakultetas.cpp" is the definition of the
Spausdinti() function:
1 2 3 4 5 6 7 8
|
// Fakultetas.cpp
#include "Fakultetas.h"
string Fakultetas::Spausdinti()
{
// stuff goes here (see below)
}
|
One of the problems with your code is that things are not named very well. (Don't feel bad, it takes some time to get used to giving things good names.)
A function named "Print" would normally be expected to output to a stream. But your function should actually return a
string that can be printed. Just like you do in the student class.
Instead, you have a function in "Main.cpp" that is doing that. Move the stuff from "Main.cpp" into "Fakultetas.cpp":
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// Fakultetas.cpp
#include "Fakultetas.h"
string Fakultetas::Spausdinti()
{
stringstream eil;
eil << " Pavarde Vardas Grupe Pazymiai " << endl;
eil << "-------------------------------------" << endl;
for(int i=0; i<Imti(); i++)
eil <<Imti(i).Spausdinti();
eil << "------------------------------------ " << endl;
return eil.str();
}
|
Now you can update the
Spausdinti() function in "Main.cpp" to just call the faculty class method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main(){
ofstream fr(FR,ios::trunc);
Fakultetas info; // duomenys // faculty info
Duomenys(info); // skaitomi duomenys // data( info )
Spausdinti(fr, info, "Pradiniai duomenys"); // print( info, "the input data" )
Fakultetas gerus_pazymius; // the new list of students (with passing grades)
Kurti(info, gerus_pazymius); // create( info, info )
Spausdinti(fr, gerus_pazymius, "Gerus Pazymius"); // print the students with passing grades
fr.close();
//system("pause");
return 0;
}
|
1 2 3 4 5 6
|
void Spausdinti(ostream & fr, Fakultetas & R, const string & antraste)
{
fr << setw(30) << antraste << endl;
fr << R.Spausdinti();
fr << endl; // (just to add a blank line between the printouts)
}
|
I hope this helps.
fixed a typo