我是C ++编码的新手。 我正试图为银行帐户创建一个菜单驱动程序,但是在从程序中获取输出后,它将引发异常。
如果有人可以用简单的术语来解释它,那将非常有帮助,因为我不了解异常抛出。
VS中的错误是:
引发未处理的异常:读取访问冲突 _Pnext为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");
}