cppbeginner10 minutes

Basic Bank Account Manager

Create a simple bank account manager that allows deposits, withdrawals, and balance checks using functions and conditionals.

Challenge prompt

Write a program in C++ to simulate a basic bank account. Create a structure or class to represent the account, containing the account owner's name and current balance. Implement the following functions: deposit funds, withdraw funds, and check balance. Withdrawals should only be allowed if there are sufficient funds. After implementing these, demonstrate the functionality by creating an account, making a deposit, attempting a withdrawal, and displaying the final balance.

Guidance

  • Use a class or struct to group account information and related functions.
  • Use conditionals to check if the withdrawal amount does not exceed the balance.
  • Write small helper functions for deposit, withdrawal, and balance display.

Hints

  • Remember to update the balance after a successful deposit or withdrawal.
  • Use 'if' statements to prevent overdrawing the account balance.
  • Keep the account owner's name as a member variable to personalize the output.

Starter code

#include <iostream>
using namespace std;

struct BankAccount {
    string owner;
    double balance;

    // Implement deposit, withdrawal, and getBalance functions
};

int main() {
    BankAccount myAccount; 
    myAccount.owner = "Alice";
    myAccount.balance = 0.0;

    // Your code to call functions and display results goes here

    return 0;
}

Expected output

Account owner: Alice Deposited: 150 Withdrawal: 100 Remaining balance: 50

Core concepts

structs/classesfunctionsconditionalsbasic I/O

Challenge a Friend

Send this duel to someone else and see if they can solve it.