0

I'm making a student management system. All is working well, except the Delete Student Info function. I'm totally new in studying the C++ language.

When I try to use the Delete function, it will result like this, like a loop:

Result of delete function code

And this is my delete code:

void student::deleted()
{
    system("cls");
    fstream file, file1;
    int found = 0;
    string snum;
    cout<<"\t\t\t\t-------------------------------------------\t\t\t"<<endl;
    cout<<"\t\t\t\t---------Delete Student Information--------\t\t\t"<<endl;
    file.open("Records.txt", ios::in);
    
    if (!file)
    {
        cout << "\n\t\t\tNo information is available.";
        file.close();
    }
    
    else
    
    {
        cout <<"\nEnter Student Number you want to remove: ";
        cin >> snum;
        file1.open("Records1.txt", ios::app | ios::out);
        file >> student_num >> name >> bday >> address >> gender >> degree >> year;
        
        while (!file.eof())
        {
            if(snum != student_num)
            {
            file1 << " " << student_num << " " << name << " " << bday << " " << address << " " << gender << " " << degree << " " << year ;
            
        } else 
        {
            found==0;
            cout <<"\n\t\t\tSuccessfully Deleted.";
        }
    }   
    file1 >> student_num >> name >> bday >>address >> gender >> degree >> year ;
        
    if (found == 0)
    {
        cout <<"\n\t\t\tStudent Number not found.";
    }
    file1.close();
    file.close();
    remove("Records.txt");
    rename("Records.txt", "NewRecords.txt");
}

All is working on my program, except this delete function. I hope you can enlighten me with knowledge I still not know.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
mr.chu
  • 1
  • 1
    [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) – Retired Ninja Dec 04 '21 at 03:53
  • 1
    It should be found = 1, and not found == 0 in the else. – Adriel Jr Dec 04 '21 at 03:56
  • 1
    Your code is hard to read due to inconsistent indentation, but it appears you read from the input file once outside the loop then loop forever because there's no reading from the input file inside the loop. There is a read after the loop, but it's never reached. – Retired Ninja Dec 04 '21 at 03:56
  • 1
    The line were you parse the file should be inside the while. – Adriel Jr Dec 04 '21 at 03:57
  • 1
    Also, I think in the rename line you want to rename Records1 and not Records – Adriel Jr Dec 04 '21 at 04:00

1 Answers1

0

Basically, your whole while loop is structured wrong. Try something more like this instead:

void student::deleted()
{
    system("cls");

    cout<<"\t\t\t\t-------------------------------------------\t\t\t"<<endl;
    cout<<"\t\t\t\t---------Delete Student Information--------\t\t\t"<<endl;
    
    ifstream inFile("Records.txt");
    if (!inFile)
    {
        cout << "\n\t\t\tCan't open file.";
        return;
    }

    ofstream outFile("NewRecords.txt");
    if (!outFile)
    {
        cout << "\n\t\t\tCan't create new file.";
        return;
    }

    cout << "\nEnter Student Number you want to remove: ";

    string snum;
    cin >> snum;
        
    bool found = false;
    while (inFile >> student_num >> name >> bday >> address >> gender >> degree >> year)
    {
        if (snum == student_num)
        {
            found = true;
        }
        else if (!(outFile << " " << student_num << " " << name << " " << bday << " " << address << " " << gender << " " << degree << " " << year)
        {
            cout << "\n\t\t\tCan't write to new file.";
            outFile.close();
            remove("NewRecords.txt");
            return;
        }
    }   

    if (!inFile.eof())
    {
        cout << "\n\t\t\tCan't read information.";
        outFile.close();
        remove("NewRecords.txt");
        return;
    }

    outFile.close();
    inFile.close();

    if (!found)
    {
        cout <<"\n\t\t\tStudent Number not found.";
        remove("NewRecords.txt");
        return;
    }

    if (rename("Records.txt", "OldRecords.txt") != 0)
    {
        cout <<"\n\t\t\tCan't backup old file.";
        remove("NewRecords.txt");
        return;
    }

    if (rename("NewRecords.txt", "Records.txt") != 0)
    {
        cout <<"\n\t\t\tCan't rename new file.";
        rename("OldRecords.txt", "Records.txt");
        return;
    }

    remove("OldRecords.txt");

    cout <<"\n\t\t\tSuccessfully Deleted.";
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770