9 Replies - 475 Views - Last Post: 20 September 2019 - 07:07 AM Rate Topic: -----

#1 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

c++ Savings Account using classes question

Posted 12 September 2019 - 02:53 PM

I'm almost done with my banking system, but I'm trying to figure out how to set up the total_money formula correctly for depositing and withdrawing to keep track on how much money is in the account.

#include <iostream>
using namespace std;

class SavingsAccount
{
public:
	SavingsAccount();
	SavingsAccount(int d, int c);
	void deposit(int d, int c);
	void withdraw(int d, int c);
	void total(int dollars, int cents);
	void set(int dollars, int cents);
	void convert(int d, int c);
private:
	double total_money;
	int dollars;
	int cents;
	int D_balance;
};

SavingsAccount::SavingsAccount()
{
	dollars = 0;
	cents = 0;
}
void SavingsAccount::set(int dollars, int cents)
{

}

int main()
{
	SavingsAccount bank1, bank2(50, 88);
	int dollars, cents;
	bank1.set(30, 65);//setting bank1 to have initial money as I call upon the void set function above
	bank2.set(0, 0);
	int anwser;
	cout << "Would you like to 1.Deposit or 2.Withdraw?";
	cin >> anwser;
	if (anwser == 1)
	{
		cout << "Enter in how much you want to deposit in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to deposit in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.deposit(dollars, cents);//should call upon the seprate deposit funtction below
	}
	else if (anwser == 2)
	{
		cout << "Enter in how much you want to withdraw in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to withdraw in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.withdraw(dollars, cents);
	}

}
SavingsAccount::SavingsAccount(int d, int c)
{
	dollars = d;
	cents = c;

}
void SavingsAccount::convert(int d, int c)
{
	
	dollars = dollars + c / 100;
	cents = c % 100;//% used for the remainer
}


void SavingsAccount::deposit(int d, int c)
{
	dollars += d;//d is where the program keeps track of the values
	cents += c;
	if (cents >= 100)
	{
		convert(d, c);
	}
	//total_money=???
	total(dollars, cents);
}

void SavingsAccount::withdraw(int d, int c)
{
	dollars -= d;
	cents -= c;
	if (cents >= 100)
	{
		convert(c, d);
	}

	total(dollars, cents);
}

void SavingsAccount::total(int dollars, int cents)
{
	cout << "Total money: " << total_money << endl;

}




Is This A Good Question/Topic? 0
  • +

Replies To: c++ Savings Account using classes question

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5485
  • View blog
  • Posts: 14,448
  • Joined: 18-April 07

Re: c++ Savings Account using classes question

Posted 12 September 2019 - 03:13 PM

I am not sure why you have total_money if you can just have "balance". As for the formula, how do you deposit money? You take the current balance, add the number of dollars and cents to it and that is your new balance. dollars plus cents is a double right? 5 dollars and 23 cents is 5.23. That is what you add to your double total_money

double sumDollarsCents = dollars + ((double)cents / 100.0);
total_money = total_money + sumDollarsCents;



I would also make your total method just be a print the total_money value, so don't have it take any parameters.

#include <iostream>
using namespace std;

class SavingsAccount
{
public:
	SavingsAccount();
	SavingsAccount(int d, int c);
	void deposit(int d, int c);
	void withdraw(int d, int c);
	void total();
	void set(int dollars, int cents);
	void convert(int d, int c);
private:
	double total_money;
	int dollars;
	int cents;
	int D_balance;
};

SavingsAccount::SavingsAccount()
{
	dollars = 0;
	cents = 0;
}
void SavingsAccount::set(int dollars, int cents)
{

}

int main()
{
	SavingsAccount bank1, bank2(50, 88);
	int dollars, cents;
	bank1.set(30, 65);//setting bank1 to have initial money as I call upon the void set function above
	bank2.set(0, 0);
	int anwser;
	cout << "Would you like to 1.Deposit or 2.Withdraw?";
	cin >> anwser;
	if (anwser == 1)
	{
		cout << "Enter in how much you want to deposit in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to deposit in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.deposit(dollars, cents);//should call upon the seprate deposit funtction below
	}
	else if (anwser == 2)
	{
		cout << "Enter in how much you want to withdraw in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to withdraw in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.withdraw(dollars, cents);
	}

}
SavingsAccount::SavingsAccount(int d, int c)
{
	dollars = d;
	cents = c;

}
void SavingsAccount::convert(int d, int c)
{
	
	dollars = dollars + c / 100;
	cents = c % 100;//% used for the remainer
}


void SavingsAccount::deposit(int d, int c)
{
	dollars += d;//d is where the program keeps track of the values
	cents += c;
	if (cents >= 100)
	{
		convert(d, c);
	}
	
  double sumDollarsCents = dollars + ((double)cents / 100.0);
  total_money = total_money + sumDollarsCents;
  total();
}

void SavingsAccount::withdraw(int d, int c)
{
	dollars -= d;
	cents -= c;
	if (cents >= 100)
	{
		convert(c, d);
	}
  total();
}

void SavingsAccount::total()
{
	cout << "Total money: " << total_money << endl;

}



Now make sure that your input values are valid and also make sure that they can't take out more than they have. Unless you allow a negative balance. :)
Was This Post Helpful? 0
  • +
  • -

#3 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: c++ Savings Account using classes question

Posted 12 September 2019 - 03:30 PM

I'm not sure I follow because I tried doing what you suggested,but didn't work.

This post has been edited by Skydiver: 13 September 2019 - 06:37 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: c++ Savings Account using classes question

Posted 12 September 2019 - 07:41 PM

Why do you even have a "total_money" variable in your class? Isn't total money just dollars + cents?

By the way what is D_balance supposed to be representing?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: c++ Savings Account using classes question

Posted 12 September 2019 - 07:56 PM

Sorry for the D_balance, I deleted that out so disregard that. As for the total_money I've been trying to use that to keep track of the money in the bank account. So for example, when I deposit 30 dollars and go back in to deposit 10 more dollars I should get 40. I hope that makes sense.

This post has been edited by Skydiver: 13 September 2019 - 06:36 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: c++ Savings Account using classes question

Posted 12 September 2019 - 08:05 PM

Quote

As for the total_money I've been trying to use that to keep track of the money in the bank account.

Again why do you even need total_money? The balance (total_money) is just the sum of dollars and cents.

Also posted here.

Jim
Was This Post Helpful? 2
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7093
  • View blog
  • Posts: 24,102
  • Joined: 05-May 12

Re: c++ Savings Account using classes question

Posted 13 September 2019 - 06:39 AM

Deadweight77: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area. If you must quote, limit it down to the particular thing you want to address.
Was This Post Helpful? 0
  • +
  • -

#8 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: c++ Savings Account using classes question

Posted 18 September 2019 - 01:57 PM

Sorry for the late response. I've been putting this project on the back burner and focused on a couple other projects. So I revised the code here. As mentioned before, total_money is just the same as balance in a bank account. I'm able to put in money and convert if needed and displays it fine, but still struggling on how to keep money in the program when depositing and later withdraw. Say I want to deposit $65.43 then deposit $10. I should get $75.43, but I don't. When I enter in the money it will only display what I just put in instead of adding or subtracting what should be in the balance (or in this case total_money).

#include <iostream>
using namespace std;

class SavingsAccount
{
public:
	SavingsAccount();
	SavingsAccount(int d, int c);
	void deposit(int d, int c);
	void withdraw(int d, int c);
	void total();
	void set(int dol, int cen);
	void convert(int d, int c);
private:
	double total_money;
	int dollars;
	int cents;
};

SavingsAccount::SavingsAccount()
{
	dollars = 0;
	cents = 0;

}
void SavingsAccount::set(int dol, int cen)
{

}

int main()
{
	SavingsAccount bank1, bank2(50, 88);
	int dollars, cents;
	bank1.set(30, 65);
	bank2.set(0, 0);
	int anwser;
	cout << "Would you like to 1.Deposit or 2.Withdraw?";
	cin >> anwser;
	if (anwser == 1)
	{
		cout << "Enter in how much you want to deposit in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to deposit in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.deposit(dollars, cents);//should call upon the seprate deposit funtction below
		bank1.total();
	}
	else if (anwser == 2)
	{
		cout << "Enter in how much you want to withdraw in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to withdraw in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.withdraw(dollars, cents);
		bank1.total();
	}

}
SavingsAccount::SavingsAccount(int d, int c)
{
	dollars = d;
	cents = c;

}
void SavingsAccount::convert(int d, int c)
{

	dollars = dollars + c / 100;
	cents = c % 100;//% used for the remainer
}


void SavingsAccount::deposit(int d, int c)
{
	dollars += d;//d is where the program keeps track of the values of dollars
	cents += c;
	if (cents >= 100)
	{
		convert(d, c);
	}
	total_money = dollars + cents * .01;


}

void SavingsAccount::withdraw(int d, int c)
{
	dollars -= d;
	cents -= c;
	if (cents >= 100)
	{
		convert(c, d);
	}
	total_money = dollars + cents * .01;

}

void SavingsAccount::total()
{

	cout << "Total money: " << total_money << endl;
}



Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7093
  • View blog
  • Posts: 24,102
  • Joined: 05-May 12

Re: c++ Savings Account using classes question

Posted 18 September 2019 - 02:32 PM

As an aside, instead of constantly passing around dollars and cents like that, you should declare a class like this quick and dirty implementation:
class Money
{
public:
    Money(int dollars = 0, int cents = 0)
        : m_dollars(dollars), m_cents(cents)
    {
    }

    Money(const Money& value)
        : m_dollars(value.GetDollars()), m_cents(value.GetCents())
    {
    }

    void Add(const Money& value)
    {
        // your implementation here
    }

    void Subtract(const Money& value)
    {
        // your implementation here
    }

    std::string GetDisplayValue() const
    {
        // your implementation here
    }

    int GetDollars() const
    {
        return m_dollars;
    }

    int GetCents() const
    {
        return m_cents;
    }

private:
    int m_dollars;
    int m_cents;
}


Was This Post Helpful? 0
  • +
  • -

#10 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: c++ Savings Account using classes question

Posted 20 September 2019 - 07:07 AM

I was able to figure it out by doing a do while loop for the main function. I'll put it here so others may stumble upon it and learn.

#include <iostream>
using namespace std;

class SavingsAccount
{
public:
	SavingsAccount();
	SavingsAccount(int d, int c);
	void deposit(int d, int c);
	void withdraw(int d, int c);
	void total();
	void set(int dol, int cen);
	void convert(int d, int c);
private:
	double total_money;
	int dollars;
	int cents;
};

SavingsAccount::SavingsAccount()
{
	dollars = 0;
	cents = 0;

}
void SavingsAccount::set(int dol, int cen)
{

}

int main()
{
	SavingsAccount bank1, bank2(50, 88);
	int dollars, cents, anwser;
	bank1.set(30, 65);
	bank2.set(0, 0);
	do
	{
		cout << "Would you like to 1.Deposit or 2.Withdraw?" << endl;
		cin >> anwser;
		if (anwser == 1)//if 1 is the anwser we go through this block 
		{
			cout << "Enter in how much you want to deposit in dollars:";
			cin >> dollars;
			cout << "Enter in how much you want to deposit in cents:";
			cin >> cents;
			if (dollars < 0 || cents < 0)
			{
				cerr << "Invalid!" << endl;//if the entered values are negitive it won't take that
				exit(1);
			}
			bank1.deposit(dollars, cents);//should call upon the seprate deposit funtction below
			bank1.total();//calling to the total function and display the balance
		}
		else if (anwser == 2)//if we chose 2 we go through this block instead of the one above
		{
			cout << "Enter in how much you want to withdraw in dollars:";
			cin >> dollars;
			cout << "Enter in how much you want to withdraw in cents:";
			cin >> cents;
			if (dollars < 0 || cents < 0)//no negitive values allowed 
			{
				cerr << "Invalid!" << endl;
				exit(1);
			}
			bank1.withdraw(dollars, cents);//should call upon the seprate withdraw funtction below
			bank1.total();
		}

		cout << "would you like to go again? 1. yes or 2. no" << endl;
		cin >> anwser;
	} while (anwser == 1);//we go through this whole thing again if 1 is pressed and will add or subtract to the existing values put in previuosly 

	return 0;
}
SavingsAccount::SavingsAccount(int d, int c)
{
	dollars = d;
	cents = c;

}
void SavingsAccount::convert(int d, int c)
{

	dollars = dollars + c / 100;
	cents = c % 100;//% used for the remainer
}


void SavingsAccount::deposit(int d, int c)
{

	dollars += d;//d is where the program keeps track of the values of dollars and adds
	cents += c;
	if (cents >= 100)
	{
		convert(d, c);
	}

	total_money = dollars + cents * .01;//sorts out total money to present it in dollars and cents properly


}

void SavingsAccount::withdraw(int d, int c)
{
	dollars -= d;//subtracts from balance 
	cents -= c;
	if (cents >= 100)
	{
		convert(c, d);
	}
	total_money = dollars + cents * .01;

}

void SavingsAccount::total()
{

	cout << "Total money: " << total_money << endl;

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1