Take the 2-minute tour ×

This is my first time posting to this site, therefore I would appreciate all criticisms in a constructive manner. I am learning to write Object Oriented programming with C++ and decided to make a trivial simulation of an ATM machine that takes in user input and processes it (i.e makeDeposit, checkBalance, etc.).

My Problem: BankAccount method: makeDeposit end up changing the output of getBalance() to prefix with a zero: for example: makeDeposit(10) will change getBalance() from 0 -> 010.

#include <string>
class BankAccount {

    float m_accountBalance;
    BankAccount ();
    float getBalance();

    /*Can I pass by value or would reference be necessary?*/
    void makeDeposit(BankAccount&, int);



#include "BankAccount.h"

#include <iostream> //remove once done
using namespace std; //remove once done

BankAccount::BankAccount () {
    m_accountBalance = 0;

float BankAccount::getBalance() {
    return m_accountBalance;

//increment the object balance based on ammount
void BankAccount::makeDeposit(BankAccount &bao, int deposit_amount) {
    bao.m_accountBalance += deposit_amount;

int main () {
    BankAccount b1;
    cout << b1.getBalance(); //returns 0
    b1.makeDeposit (b1,1); //returns 01
    cout << b1.getBalance();
    //makeDepost(b2, 10);
    //b2.getBalance(); will retrun 010

    return 0;

Any tips would also be much appreciated as I am about to take the roughest Data Structures course with little experience with object oriented programming.

Also, could someone explain to me what subversion is and how it is used.

share|improve this question
Hi, welcome to SO. Please post one question at the time. "subversion" is totally out of the scope of the title of this question. +1 for a short to the point example. –  jrok Sep 8 '12 at 9:09
You will get suggestions to use std::endl. Bear in mind that it flushes the output sequence. You might not need that, in which case you can just use a newline character, "\n". –  juanchopanza Sep 8 '12 at 9:15
I kind of lost you at "flushes the output sequence". Does this have to do with program speed? –  user1656421 Sep 8 '12 at 9:19
@user61593 it can affect the speed, yes. But in general, there is no need to flush every time you want a new line. –  juanchopanza Sep 8 '12 at 9:21

2 Answers 2

up vote 6 down vote accepted

You just print the 0 value to the console, then add the 1 to the balance and again prints the balance to the console. Result? 01.

How to avoid it? Add << endl to the end of the every cout. You will print every value in separate line then. Example:
cout << b1.getBalance(); change to cout << b1.getBalance() << endl;

Your second question (altough it's not related with the first one):
Subversion (called also SVN) is a software versioning system. You can do backups often with it and - before everything - you can work with your project collaboratively with your team. More info: http://en.wikipedia.org/wiki/Apache_Subversion

share|improve this answer
Hahaha wow, I feel like I should have noticed that right away. Thanks for the quick reply! –  user1656421 Sep 8 '12 at 9:12
No problem :) If my answer is enough for you, click that sign below the question rating at the left of it to accept my answer as correct :P –  m4tx Sep 8 '12 at 9:14

You're not printing a new line so it concentrates it together as 0 then 1 for 01. To fix it call endl; after cout << b1.getBalance();

Like so:

int main () {
    BankAccount b1;
    cout << b1.getBalance() << endl; //returns 0
    b1.makeDeposit (b1,1); //returns 01
    cout << b1.getBalance() << endl;
    //makeDepost(b2, 10);
    //b2.getBalance(); will retrun 010

    return 0;
share|improve this answer

Your Answer

