我正在使用 C++ 制作一个简单的银行管理系统,但我遇到了一些问题。我刚刚了解了课程,我知道它可以用来实现账户余额、存款、取款等功能,但我遇到了困难。我不明白该怎么做。有人可以简单地向我解释一下吗,我是初学者。
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;
//This function gives a Bank Account class
class BankAccount {
double account_balance;
public:
void set_balance(double initial_balance = 0.0) {
account_balance = initial_balance;
}
void deposit(double deposit_amount)
{
account_balance += deposit_amount;
}
}
int main() {
int Option1 = 1, Option2 = 2, Option3 = 3, Option4 = 4, account_Options;
string user_Name, user_Password;
srand(time(NULL));
int account_No = rand() % 10000000000 + 1000000000;
cout << "Welcome to Scam Bank \n" // Welcome message and Options to choose New account or Proceed to an account.
"1.Open New Account \n"
"2.Continue to my Account \n"
"How would you like to proceed?...\n";
cin >> account_Options;
if (account_Options == Option1)
{ // Option for New user.
cout << "Welcome, New user \n";
cout << "Please type your First name:... \n";
cin >> user_Name;
cout << "Please type a password:... \n";
cin >> user_Password;
cout << "Hi " << user_Name << ", Thanks for creating an accout with us!" << endl;
cout << "Your account Number is: " << account_No;
while (true)
{
double deposit_amount;
cout << "How would you like to procceed?" << endl;
cout << "1.Deposit \n"
"2.Withdraw \n"
"3.Check Balance \n"
"4.Exit \n";
cin >> account_Options;
const int option1= 1, option2 = 2, option3 = 3, option4 = 4;
switch (account_Options) {
case option1:
cout << "How much would you like to deposit" << endl;
cin >> deposit_amount;
cout << "You have successfully deposit $" << << " to your balance." << endl;
break;
case option2:
int withdraw;
cout << "How much would you like to withdraw? \n";
cin >> withdraw;
cout << "You have withdrawn: " << withdraw << endl;
break;
case option3:
cout << "Your current balance is: " << newAccount.bank_balance << endl;
break;
case option4:
cout << "Thank you for using Scam Bank!" << endl;
return 0;
break;
default:
cout << "Invalid Option" << endl;
break;
}
}
} return 0;
}
我已经尝试过没有类的存款、余额和取款功能,但它不会针对任何新输入进行更新。例如,如果我存入 200 美元,余额将为 200 美元,当我再存入 200 美元时,余额仍为 200 美元,而不是更新后的 400 美元。
It seems like you have in the BankAccount class declaration, you're missing a semicolon after the class definition.
And also in the deposit logic, you're not updating the balance correctly.
Here is the fully updated code
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
class BankAccount {
private:
double account_balance;
public:
void set_balance(double initial_balance = 0.0) {
account_balance = initial_balance;
}
void deposit(double deposit_amount) {
account_balance += deposit_amount;
}
void withdraw(double withdraw_amount) {
if (withdraw_amount <= account_balance) {
account_balance -= withdraw_amount;
} else {
cout << "Insufficient funds!" << endl;
}
}
double get_balance() {
return account_balance;
}
};
int main() {
int Option1 = 1, Option2 = 2, Option3 = 3, Option4 = 4,
account_Options;
string user_Name, user_Password;
srand(time(NULL));
int account_No = rand() % 10000000000 + 1000000000;
cout << "Welcome to Scam Bank \n"
<< "1.Open New Account \n"
<< "2.Continue to my Account \n"
<< "How would you like to proceed?...\n";
cin >> account_Options;
if (account_Options == Option1) {
cout << "Welcome, New user \n";
cout << "Please type your First name:... \n";
cin >> user_Name;
cout << "Please type a password:... \n";
cin >> user_Password;
cout << "Hi " << user_Name << ", Thanks for creating an account
with us!" << endl;
cout << "Your account Number is: " << account_No << endl;
BankAccount newAccount;
double deposit_amount, withdraw_amount;
while (true) {
cout << "How would you like to proceed?" << endl
<< "1.Deposit \n"
<< "2.Withdraw \n"
<< "3.Check Balance \n"
<< "4.Exit \n";
cin >> account_Options;
switch (account_Options) {
case 1:
cout << "How much would you like to deposit? ";
cin >> deposit_amount;
newAccount.deposit(deposit_amount);
cout << "You have successfully deposited $" <<
deposit_amount << " to your balance." << endl;
break;
case 2:
cout << "How much would you like to withdraw? ";
cin >> withdraw_amount;
newAccount.withdraw(withdraw_amount);
break;
case 3:
cout << "Your current balance is: $" << fixed <<
setprecision(2) << newAccount.get_balance() << endl;
break;
case 4:
cout << "Thank you for using Scam Bank!" << endl;
return 0;
default:
cout << "Invalid Option" << endl;
break;
}
}
}
return 0;
}
Thank You