Why is my program printing the last record of a file twice?

  c++

I created a simple bank application program to ask a user whether they want to add a bank record to a file or show all the records available. Both these functions are facilitated by write_rec() and read_rec() respectively. But when the function read_rec() is applied, while it does print all the records available in the file(a single file is used to store all the records), for some reason it prints the last record in the file two times instead of just once. It’s very frustrating to see everything works so well then this problem pops up out of nowhere. I tried to see where the issue is but for the life of me I just can’t find it. Can you guys please help me with this one?

Here’s the code:

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

class account_query
{
    char account_number[20];
    char firstName[10];
    char lastName[10];
    float total_Balance;
public:
    void set_data();
    void show_data();
    void write_rec();
    void read_rec();
};

void account_query::set_data()
{
    cout<<"nEnter Account Number: ";
    cin>>account_number;
    cout<<"Enter First Name: ";
    cin>>firstName;
    cout<<"Enter Last Name: ";
    cin>>lastName;
    cout<<"Enter Balance: ";
    cin>>total_Balance;
    cout<<endl;
}

void account_query::show_data()
{
    cout<<"Account Number: "<<account_number<<endl;
    cout<<"First Name: "<<firstName<<endl;
    cout<<"Last Name: "<<lastName<<endl;
    cout<<"Current Balance: Rs.  "<<total_Balance<<endl;
    cout<<"-------------------------------"<<endl;
}

void account_query::write_rec()
{
    ofstream outfile;
    outfile.open("D:/rec.bin", ios::binary|ios::in|ios::out|ios::app);
    set_data();
    outfile.write(reinterpret_cast<char *>(this), sizeof(*this));
    outfile.close();
}

void account_query::read_rec()
{
    ifstream outfile;
    outfile.open("D:/rec.bin", ios::binary);
    if(!outfile.is_open())
    {
        cout << "Error! File not found!" << endl;
        return;
    }
    cout << "n****Data from file****" << endl;
    while(outfile.good())
    {
    outfile.read(reinterpret_cast<char *>(this), sizeof(*this));
    show_data();
    }
     outfile.close();
    }

int main()
{
account_query A;
    int choice;
    cout << "***Account Information System***" << endl;
    while(true)
    {
        cout << "Select one option below";
        cout << "nt1-->Add record to file";
        cout << "nt2-->Show record from file";
        cout << "nt3-->Quit";
        cout << "nEnter you chice: ";
        cin >> choice;
        switch(choice)
        {
            case 1:
            A.write_rec();
            break;
            case 2:
            A.read_rec();
            break;
            case 3:
            exit(0);
            break;
            default:
            cout << "nEnter correct choice";
            exit(0);
        }
 }
    system("pause");
    return 0;
}

And this is the result I get when I try to print the record on the console:

enter image description here

Please help me 🥺

Source: Windows Questions C++

LEAVE A COMMENT