0

I'm new to c++ coding. I'm tring to create a menu driven program for a banking account but it throws and exception after getting an output from the program.

If someone can please explain it in simple terms it would be really helpful since I have no knowledge about exception throwing.

The error in VS was:

Unhandled exception thrown: read access violation _Pnext was 0xB46A2C

void Data::AccData()
{
    cout << "Enter New Account Number : ";
    cin >> accountnumber;
    cout << "Enter Username : ";
    cin >> username;
b:
    cout << "Enter Account Type (C/S) : ";
    cin >> choice;
    choice = toupper(choice);
a:
    if (choice == 'C')
    {
        cout << "Enter Initial Balance : ";
        cin >> balance;
        if (balance < 1000)
        {
            cout << "Initial balance should be greater than 1000$ for Checking account! ";
            this_thread::sleep_for(chrono::seconds(2));
            system("cls");
            goto a;
        }
    }
    else if (choice == 'S')
    {
        cout << "Enter Initial Balance : ";
        cin >> balance;
        if (balance < 500)
        {
            cout << "Initial balance should be greater than 500$ for Saving account! ";
            this_thread::sleep_for(chrono::seconds(2));
            system("cls");
            goto a;
        }
    }
    else
    {
        cout << "Error! Choose S or C. ";
        this_thread::sleep_for(chrono::seconds(2));
        system("cls");
        goto b;
    }
    system("cls");
}

void Data::ShowAccData()
{
    cout << "Account Number : " << accountnumber << endl;

    cout << "Account Information ." << "\n" << endl;

    cout << "Account Number : " << accountnumber << endl;
    cout << "Name of Account Holder : " << username << endl;
    cout << "Account Type : " << choice << endl;
    cout << "Account Balance : " << balance << endl;

    system("cls");
}

void Data::ShowAccDataAll()
{
    cout << accountnumber << "\t\t " << username << "\t\t " << choice << "\t\t" << balance << endl;
}


void Input()
{
    Data d;
    ofstream file("data.txt", ios::binary | ios::app);
    d.AccData();
    file.write(reinterpret_cast <char*> (&d), sizeof(Data));
    file.close();
}

void Output()
{
    Data d;
    ifstream file("data.txt", ios::binary);
    while (file.read(reinterpret_cast <char*> (&d), sizeof(Data)))
    {
        d.ShowAccData();
    }
    file.close();
}

void Search()
{
    Data d;
    int acc;
    cout << "Input account number : ";
    cin >> acc;
    ifstream file("data.txt", ios::binary);
    while (file.read(reinterpret_cast <char*> (&d), sizeof(Data)))
    {
        if (d.accountnumber == acc)
        {
            d.ShowAccData();
        }
    }
}

void Display_All()
{
    Data d;
    ifstream file("data.txt", ios::binary);
    cout << "\n\n\t\tACCOUNT HOLDER LIST\n\n";
    cout << "====================================================\n";
    cout << "A/c no.      NAME           Type             Balance\n";
    cout << "====================================================\n";
    while (file.read(reinterpret_cast <char*> (&d), sizeof(Data)))
    {
        d.ShowAccDataAll();
    }
    file.close();

    system("pause");
}

void Intro();
void Menu();


int main()
{
    Intro();
    Menu();

    int ch;
    do
    {
        cout << "\nPlease Enter the option that u wish : ";
        cin >> ch;
        if (ch == 1)
        {
            Input();
        }
        else if (ch == 2)
        {
            Output();
        }
        else if (ch == 3)
        {
            Search();
        }
        else if (ch == 4)
        {
            Display_All();
        }
        else
        {
            cout << "Choose valid options";
            this_thread::sleep_for(chrono::seconds(2));
            system("cls");
            Menu();
        }
        system("cls");
        cin.get();
    } while (ch != 8);
}


void Intro()
{
    cout << "============================================";
    cout << "\n********************************************";
    cout << "\n\n\n\t\t  BANK";
    cout << "\n\n\t\tMANAGEMENT";
    cout << "\n\n\t\t  SYSTEM";
    cout << "\n\n\n\nMADE BY : Sakchyam Shrestha";
    cout << "\nThe British College, Kathmandu";
    cout << "\n********************************************";
    cout << "\n============================================";

    cin.get();
    system("cls");
}

void Menu()
{
    cout << "==================================" << endl;
    cout << "**********************************" << endl;
    cout << "\n1.Create Account." << endl;
    cout << "2.Modify Account." << endl;
    cout << "3.Show All Account Information." << endl;
    cout << "8.Exit\n" << endl;
    cout << "==================================" << endl;
    cout << "**********************************" << endl;

    system("cls");
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53

0 Answers0