3

I have been working on a trivial assignment to get used to coding. I am designing an ATM machine and at the moment it is composed of 2 classes:

  1. BankAccount.cpp

    • Constructor for different types of account
    • Only has balance as a member
  2. Transaction.cpp

    • Performs a method on the BankAccount (i.e make deposit, make withdrawl & get balance)

Problem: BankAccount is automatically initialized to a balance of 10 which is undesired. So for example, if I made a checking account and chose to deposit $10, balance would print out $20.

//BankAccount.h
//This class will simply take in a bank account
//with a balance, other classes will use a bank account object
//such as saving/checkings and perform operations on the 
//balance

#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H
class BankAccount {

private:
    float balance;
public:
    BankAccount ();
    float getBalance ();
    void makeDeposit ();
    void makeWithdrawl ();

};

#endif

//BankAccount.cpp
#include "BankAccount.h"
#include <iostream> //remove once done   *not to self
using namespace std; //remove once done *note to self


BankAccount::BankAccount() {
    balance = 0.00;
}

float BankAccount::getBalance() {
    return balance;
}

void BankAccount::makeDeposit() {
    cout << "How much would you like to deposit: ";
    float deposit_value;
    cin >> deposit_value;
    balance += deposit_value;
}

void BankAccount::makeWithdrawl() {
    cout << "How much would you like to withdrawl: ";
    float withdrawl_value;
    cin >> withdrawl_value;
    balance -= withdrawl_value;
}

//Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H

class Transaction {
private:
    BankAccount m_bao;
public:
    Transaction(BankAccount&);
    void displayOptions();  
    void printReciept();
};

#endif

//Transaction.cpp
#include "BankAccount.h"
#include "Transaction.h"
#include <iostream>
using namespace std;

Transaction::Transaction(BankAccount& bao) {
    m_bao = bao;
}

void Transaction::displayOptions() {
    cout << "\nPlease make a choice\n\n";
    cout << "1: Make Deposit\n";
    cout << "2: Make Withdrawl\n";
    cout << "3: Check Balance\n";

    int choice;
    cin >> choice;
    switch (choice) {
    case 1: 
        m_bao.makeDeposit();
        break;
    case 2:
        m_bao.makeWithdrawl();
        break;
    case 3:
        m_bao.getBalance();
        break;
    }
}

void Transaction::printReciept() {
    cout << "Current balance is now: " << m_bao.getBalance() + '\n';
}


int main () {

    BankAccount checking;
    Transaction q(checking);
    q.displayOptions(); 
    q.printReciept();


}

I am sure the answer is right in front of my eyes, but my brain is just fried right now. I will continue to look for the solutions and let you guys know if my problem has been solved yet.

[EDIT]

Alright, now I am trying to make it so that the customer can choose to perform transactions on either Checking or Savings account. Currently I got it looking like this in my main():

int main () {

    BankAccount checking(0.00);
    BankAccount savings(0.00);
    Transaction c(checking);
    Transaction s(savings);
    for(int i = 0; i < 10 ; i++) {
        cout << "Make an option" << endl;
        cout << "1. Checking "   << endl;
        cout << "2. Savings"     << endl;

        int choice;
        cin >> choice;
        if (choice == 1) {
            c.prompt();
            c.printReciept();
        }
        else {
            s.prompt();
            s.printReciept();
        }
    }

}

It works fine, but I would like to make this process more OOP-alized, if that makes sense :)

One option I was trying to look into was making a prompt function which would belong to Transaction.cpp. This would do everything that is done in main, except initializing the objects of course.

Maxim
  • 725
  • 1
  • 8
  • 24

1 Answers1

5

Your problem is this line:

cout << "Current balance is now: " << m_bao.getBalance() + '\n';

Which the compiler sees as:

cout << "Current balance is now: " << (m_bao.getBalance() + '\n');

'\n' is 10 as an int, so you get this:

cout << "Current balance is now: " << (m_bao.getBalance() + 10);

You probably meant to do this:

cout << "Current balance is now: " << m_bao.getBalance() << '\n';

Remember that in C++, + almost always means "add these two numbers".

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • 1
    More to the point, try: cout << "Current balance is now: " << m_bao.getBalance() << endl; – WhozCraig Sep 09 '12 at 04:00
  • 1
    @CraigNelson Depends on if they want to flush the output or not. Ending the line with `'\n'` is completely valid if you don't care when the output shows up. – Brendan Long Sep 09 '12 at 04:02
  • Really, cause I always use it on the off chance the terminal uses crlf vs. cr vs. lf. You don't know, and using endl, you don't have to know. its one of the reasons why its there. I never thought of it as flushing the stream, 'cause when i want that, I fire flush(). – WhozCraig Sep 09 '12 at 04:04
  • Well that answered my question and like I thought, it was definitely something simple. Anyways, I wish I could understand about all this flushing, but that seems a little out of scope with what I am doing at the moment. Thank you for the quick reply! – Maxim Sep 09 '12 at 04:08
  • 1
    @Ihatesyntaxerrors In your case, Craig is probably correct that you should prefer `endl`. Flushing the buffer means that the output will appear immediately. If you have *a lot* of output, it can slow your program down, but it's extremely useful for debugging, and for having predictable output. Basically, use `endl` unless you have a good reason not to. – Brendan Long Sep 09 '12 at 04:11
  • @BrendanLong yes, I concur. I was just saying the flush wasn't the reason I used endl, though it is an attribute of the manipulator, i completely agree. I use it to at least raise my odds of not getting an unexpected terminal output. Thanks for reminding about the flush though, some don't take that into account. nice. – WhozCraig Sep 09 '12 at 04:12
  • 2
    @CraigNelson: If you output `'\n'`, it will be automatically translated into the correct line-ending for the hosting environment. See [here](http://stackoverflow.com/questions/5654067/how-to-make-cout-behave-as-in-binary-mode) for a partial explanation. (EDIT: Only for streams that were not opened for binary output) – dreamlax Sep 09 '12 at 04:19
  • Alright, now I am stuck on another part, I am about to edit my question so check back shortly! – Maxim Sep 09 '12 at 04:34
  • @Ihatesyntaxerrors Please create a new question. It makes it a lot easier to answer if we can handle one problem per question (especially since I'm about to go to bed so I may not be able to add it to my answer). You'll also get help faster with a new question, since most people will ignore this one now that there's an accepted answer. – Brendan Long Sep 09 '12 at 04:35
  • @BrendanLong, thank you for the advice. I will ask another question, but I thought it would be easier here since people have already seen the problem in context. – Maxim Sep 09 '12 at 04:43