0

I am making a program in turbo C++ to create student records and modify/delete them in a binary file on user's command.

The main class that is needed to know is the student class:

class student
{
private:
    int roll_no;
    char name[50];
    academic ac;
    co_curricular cc;

    void calculate();

public:
    int get_data(int);
    void show_data();
    void show_tabular();

    int ret_roll_no();
};

There is some problem with the get_data() function, specially in the part where the roll number is assigned. The logic to assign the roll number is:

student temp;
fstream fp;
roll_no = random(9000) + 1000;

//Checking if roll number is unique
fp.open("STUDENT.DAT", ios::in);
while(!fp.eof())
{
    fp.read((char*)&temp, sizeof(temp));
    if(roll_no == temp.ret_roll_no())
        roll_no = random(9000) + 1000;  //Set roll number to another random value
}
fp.close();

Binary file STUDENT.DAT already exists, but the code doesn't go after the loop. It is somehow stuck.

Please help

Darsh K
  • 41
  • 1
  • 9
  • I don't know if this is it but https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – Jerry Jeremiah Dec 12 '19 at 04:20
  • Hang on. You are reading a string of bytes into some memory and you think the program will think it's valid? Was the file definitely written by the same program running on the same machine built by the same compiler? I guess it might work - maybe, but this is a way better way to do it https://stackoverflow.com/questions/37038909/c-read-write-class-from-to-binary-file – Jerry Jeremiah Dec 12 '19 at 04:25
  • Did you check for BOF and EOF before your loop? – Ross Bush Dec 12 '19 at 04:40
  • @JerryJeremiah I am writing the byte sequence at the location of object `temp`. There is no problem with taking in the input. There is something which is not allowing the loop to terminate – Darsh K Dec 12 '19 at 04:49
  • 1
    Step 1 towards sanity: *Stop* using Turbo C++. It was obsolete 20+ years ago when we got the C++98 standard. These days it's just an embarrassment and a waste of time. Noone writes C++ the way it was done with Turbo C++ any more (and haven't for decades). You are wasting your time learning with it. – Jesper Juhl Dec 22 '19 at 17:19

0 Answers0