7 Replies - 204 Views - Last Post: 08 September 2012 - 01:57 PM Rate Topic: -----

#1 factordva  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 08-September 12

function prints undesired value

Posted 08 September 2012 - 01:48 AM

Greetings community,

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.
//BankAccount.h
#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H

class BankAccount {

private:
	float m_accountBalance;
public:
	BankAccount ();
	float getBalance();
         
        //This was my second attempt to solve the problem, would it make more sense though
        //to pass value rather than reference or is it necessary?
	void makeDeposit(BankAccount&, int); 
	
};

#endif
 


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

//Constructor
BankAccount::BankAccount () {
	m_accountBalance = 0;
}

// Accessor
float BankAccount::getBalance() {
	return m_accountBalance;
}

//Mutator
void BankAccount::makeDeposit(BankAccount &bao, int deposit_amount) {
	bao.m_accountBalance += deposit_amount; //increment balance based on amount
}
	


int main () {
	BankAccount b1;
	cout << b1.getBalance(); //returns 0
	b1.makeDeposit (b1,1);
	cout << b1.getBalance();//returns 01
        BankAccount b2;
        b2.makeDeposit(b2, 100);
        b2.getBalanace(); //returns 0100

	
	
	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. :pinch:

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

Is This A Good Question/Topic? 0
  • +

Replies To: function prints undesired value

#2 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 200
  • Joined: 27-July 12

Re: function prints undesired value

Posted 08 September 2012 - 02:22 AM

Firstly , the output error you get is because you print the digits one after another, try to end your output lines with <<endl;
Like
cout<<b2.getBalance<<endl;

Also I think it's useless to have the m_accountBalance; and getBalance(); as floats as long as you will only add integers to m_accountBalance which is initialized to 0 by the constructor, you might want to rethink that and maybe change makeDeposit(BankAccount&, int); to makeDeposit(BankAccount&, float);
Was This Post Helpful? 1
  • +
  • -

#3 factordva  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 08-September 12

Re: function prints undesired value

Posted 08 September 2012 - 02:26 AM

View PostTwoOfDiamonds, on 08 September 2012 - 02:22 AM, said:

Firstly , the output error you get is because you print the digits one after another, try to end your output lines with <<endl;
Like
cout<<b2.getBalance<<endl;

Also I think it's useless to have the m_accountBalance; and getBalance(); as floats as long as you will only add integers to m_accountBalance which is initialized to 0 by the constructor, you might want to rethink that and maybe change makeDeposit(BankAccount&, int); to makeDeposit(BankAccount&, float);


I cant believe I didn't notice that, thank you for your quick reply.
Was This Post Helpful? 0
  • +
  • -

#4 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 200
  • Joined: 27-July 12

Re: function prints undesired value

Posted 08 September 2012 - 03:03 AM

My pleasure :)

Enjoy programming !
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1939
  • View blog
  • Posts: 5,774
  • Joined: 05-May 12

Re: function prints undesired value

Posted 08 September 2012 - 06:48 AM

Do you really need the first parameter to makeDeposit? Right now it allows for some strange behavior:
BankAccount checking;
BankAccount savings;

checking.makeDeposit(checking, 100);
savings.makeDeposit(savings, 100);

// Does this add to the checking or the savings?
checking.makeDeposit(savings, 50);

cout << "Checking: " << checking.getBalance() << endl;    // 100
cout << "Savings: " << savings.getBalance() << endl;      // 150


Was This Post Helpful? 1
  • +
  • -

#6 factordva  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 08-September 12

Re: function prints undesired value

Posted 08 September 2012 - 01:43 PM

@skydiver, wow thanks for your reply. I knew at first that it was redundant to add that parameter especially since it was already inside the object. Did I address my problem?
//added this and removed the first parameter
void BankAccount::makeDeposit(float deposit_amount) {
	this->m_accountBalance += deposit_amount;
}

void BankAccount::makeWithdrawl(float withdrawl_amount) {
	this->m_accountBalance -= withdrawl_amount;
}



int main () {
	BankAccount checking;
	BankAccount savings;
	checking.makeDeposit (100);
	savings.makeDeposit (200);
	cout << checking.getBalance() << '\n'; //returns 100
	cout << savings.getBalance() << '\n'; // returns 200
	
	checking.makeWithdrawl (20);
	savings.makeWithdrawl (20);
	cout << checking.getBalance() << '\n'; //returns 80
	cout << savings.getBalance() << '\n'; // returns 180
	
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1939
  • View blog
  • Posts: 5,774
  • Joined: 05-May 12

Re: function prints undesired value

Posted 08 September 2012 - 01:55 PM

Yes, that looks better.

Now you just need some sanity checks to make sure the deposits and withdrawals are greater than zero. Either throw an exception or return an error, up to you.

I leave it up to you to figure out how you want to deal with a deposit or withdrawal of an amount equal to zero. Either it's a valid transaction or it's not, it's up to you.
Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: function prints undesired value

Posted 08 September 2012 - 01:57 PM

Also beware of using floating point numbers when dealing with money. Floating point numbers are an approximation not an exact number. Also if you do stick with floating point numbers use double instead of float.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1