OK, I'm trying to make this thing work but it won't write on file. If I put only data about one student with "invisible" 4 that I added explicitly it will print him, but if I add more than one then it breaks... literally, not "break;". And it seems that it's not putting it in file .dat.
Class applications works fine, but I putted it in just in case. Hope that there won't be any error since I spend few mins trying to translate from my language to english.
Don't know where is error, might be in "this", or I might have put wrong "ios::"
I'd appreciate help since I lost 3 additional hours trying to figure why it's not working.
And compiling and building it don't pop up any error.
Code:
#include <iostream>
#include <fstream>
void input (char* chr)
{
std::cin.getline (chr 50);
if (std::cin.gcount() == 1)
std::cin.getline (chr, 50);
};
std::fstream dat;
class cstudents
{
public:
int nmbr, year_sign;
char lname_name[50];
void input_student();
void print_student(int);
};
void cstudents::input_student()
{
char more;
do
{
dat.open("Students.dat", std::ios::out | std::ios::binary);
std::cout<<"Input number of student: "; std::cin>>this->nmbr;
std::cout<<"Input last name and name of student: "; input(this->lname_name);
std::cout<<"Input year of sign of student: "; std::cin>>this->year_sign;
dat.write ((char*)this, sizeof(this));
std::cout<<"More students (y\\n)?"; std::cin>>more;
} while (jos == 'y' || jos == 'Y');
dat.close();
};
void cstudents::print_student (int nmbr){
dat.open ("Students.dat", std::ios::in | std::ios::binary);
while (1)
{
dat.read((char*)this, sizeof(this));
if (dat.eof())
break;
if (this->nmbr == nmbr)
{
std::cout<<"Number of student "<<this->nmbr<<std::endl;
std::cout<<"Last and first name: "<<this->lname_name<<std::endl;
std::cout<<"Year of sign: "<<this->year_sign<<std::endl;
};
};
dat.close();
};
//-----------------------------------------------------------------------------------------------------------
//this works, or it did worked before translation, so it still works with original version.
//-----------------------------------------------------------------------------------------------------------
class capplications
{
private:
capplications* next;
public:
int pass_app, nmbr, grade;
char name_class[50], date [12];
void input_application();
float average_grade (int);
capplications ();
};
capplications::capplications ()
{
next = NULL;
};
void capplications::input_application()
{
capplication *last = this, *another;
while (last->next)
last = last -> next;
another = new capplications;
last->next = another;
std::cout<<"Input number of application: "; std::cin>>another->pass_app;
std::cout<<"Input number of student: "; std::cin>>another->nmbr;
std::cout<<"Input name of class: "; input (another->name_class);
std::cout<<"Input when is exam: "; input (another->date);
std::cout<<"Input grade for exam: "; std::cin>>another->grade;
};
float capplications::average_grade (int nmbr)
{
int sum = 0, amount = 0;
capplications* current = this->next;
while (current)
{
if (current->nmbr == nmbr)
{
amount += current->grade;
++sum;
};
current = current->next;
};
return (amount/sum);
};
//-----------------------------------------------------------------------------------------------------------
//main
//-----------------------------------------------------------------------------------------------------------
int main()
{
int choice, nmbr;
capplications *application = new capplications;
cstudents student;
do
{
std::cout<<"1. Input data about students in file"<<std::endl;
std::cout<<"2. Input data about application"<<std::endl;
std::cout<<"3. Average grade of student"<<std::endl;
std::cout<<"9. Exit"<<std::endl;
std::cin>>choice;
switch (choice) {
case 1: students.input_student(); break;
case 2: applications->input_application(); break;
case 3:
std::cout<<"Number of student: "; std::cin>>nmbr;
students.print_student(nmbr);
std::cout<<"Average grade: " << prijave->prosjek_ocjena(mat_br) <<std::endl;
break;
case 4:std::cin>>nmbr; students.print_student(nmbr); break;
};
} while (choice != 9);
return 0;
}