家伙!这里是我的银行分配代码。当我选择案例1来检查余额时,它运行良好。但是,当我选择案例2进行存款时,程序在输入存款金额后立即崩溃。我查看了代码,但我不确定问题出在哪里。但是,该程序似乎在cpp.sh网站上执行时运行正常,并且只在Visual Studio上崩溃。是因为我的代码还是我的编译器?
#include <iostream>
#include <string>
using namespace std;
class account {
private:
double AccountActivity[200];
double balance;
int numTransactions;
public:
//declaration
};
void account::test() {
account a;
int choice;
bool menu = true;
double amount = 0;
while (menu = true) {
choice = a.getUserChoice();
switch (choice) {
case 1:
a.print();
break;
case 2:
cout << "\n\nPlease enter the amount of deposit: " << endl;
cin >> amount;
a.deposit(amount);
break;
case 3:
cout << "\n\nPlease enter the amount of withdrawal: " << endl;
cin >> amount;
a.withdraw(amount);
break;
case 4:
cout << "The largest deposit is: " << a.getLargest() << endl;
default:
cout << "Please choose from 1 to 4" << endl;
break;
}
}
}
int account::getUserChoice() {
//do something
}
double account::getLargest()
{
//do something
}
void account::print() {
cout << "Your balance is: $" << balance;
cout << "\n\n";
}
account::account() {
balance = 0;
}
void account::deposit(double amount) {
if (numTransactions < 200)
{
AccountActivity[numTransactions] = amount;
numTransactions++;
balance += amount;
}
}
void account::withdraw(double amount) {
if (balance < amount)
cout << "Insufficient balance" << endl;
else
{
if (amount >= 0 && numTransactions < 200)
{
AccountActivity[numTransactions] = -amount;
numTransactions++;
balance -= amount;
}
}
}
int main() {
//do something
}
答案 0 :(得分:2)
account::account()
未初始化。使用未初始化的值索引到数组是未定义的行为。
在您的帐户构建器numTransactions
中将while (menu = true) {
设置为0
哦是的:while(menu)
尝试menu
- 但是你不会在循环中改变while(1) {
,所以你也可以--parameter-values