1

I am working at my programming project for school and i came up with idea of "Student Manager Program".

The: name, roll, year, group, course, adress, email, contact, grade are stored as variables in class named Student.

Student's data is written by user keyboard input and saved in studentRecord.txt file, by object. How can i prevent this while loop to be a not infinite loop. I can't find solution to this problem.

'''

//Displaying Students Record Table
void Student::display() {
    system("cls");
    ifstream file;
    int total = 1;
    int x;
    cout << "\n-----------------------------Students Record Table-----------------------------" << endl;
    file.open("studentRecord.txt");
    if (!file) {
        cout << "\n\t\t\tNo Data Is Present.";
        file.close();
    }
    else {
        file >> name >> roll >> year >> group >> course >> adress >> email >> contact >> grade;
        while(!file.eof()) {
            cout << "\n\n\t\t\t Student Consecutive Number: " << total++ << endl;
            cout << "\t\t\t Student's Name: " << name << endl;
            cout << "\t\t\t Student's ID Number:  " << roll << endl;
            cout << "\t\t\t Student's Year:  " << year << endl;
            cout << "\t\t\t Student's Group:  " << group << endl;
            cout << "\t\t\t Student's Course: " << course << endl;
            cout << "\t\t\t Student's Adress: " << adress << endl;
            cout << "\t\t\t Student's Email: " << email << endl;
            cout << "\t\t\t Student's Contact: " << contact << endl;
            cout << "\t\t\t Student's Final Grade: " << grade << endl;
            file >> name >> roll >> year >> group >> course >> adress >> email >> contact >> grade;
        }
        if (total == 0) {
            cout << "\n\t\t\tNo Data Is Present.";
        }
    }
    do {
        cout << "\n\t\t\t If You Want to Back at previous page press 0 and Enter: ";
        cin >> x;
        if (x == 0) {
            file.close();
        }
    } while (x != 0);
}

'''

Jacob
  • 11
  • 1
  • 1
    Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Scheff's Cat May 11 '21 at 17:54
  • 1
    If any of your stream input operators fails then the stream is set to fail state and none of the following input operators won't read anything more. However, fail state doesn't mean the stream reached end of file. So, there you are - caught in an infinite loop. – Scheff's Cat May 11 '21 at 17:56
  • @Scheff Unfortunately no, it doesn't. – Jacob May 11 '21 at 17:58
  • @Scheff So what should i change then – Jacob May 11 '21 at 17:58
  • 1
    Please, read the link. There are multiple answers which explain the "Why not" as well as the "What to do instead". ;-) – Scheff's Cat May 11 '21 at 17:59
  • It sounds like you have a bad read operation and that is causing the stream to enter an error state in which it will never read any more data, and `EOF` will also never be reached. Add an if statement like `if(file.bad())` and print then current values so you can see which one was bad. – NathanOliver May 11 '21 at 18:00
  • Additionally, you only need a single call to `std::cout` to output any continual block. That means only one call to `std::cout` is needed to output ALL the student information. Consider opening/validating the file is open in the calling functions and pass a reference to an open fstream object as a parameter to your function. If you file open fails, there is no need to make the function call to begin with. – David C. Rankin May 11 '21 at 20:12

0 Answers0