5 Replies - 361 Views - Last Post: 07 September 2019 - 09:53 AM Rate Topic: -----

#1 Deadweight77   User is offline

  • New D.I.C Head

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

c++ Savings Account using classes question

Posted 07 September 2019 - 08:43 AM

I've been struggling with trying to keep track of money in the bank account (in this case bank1). I'm trying to figure out how to deposit and withdraw properly. I looked everywhere to no avail.

#include <iostream>
using namespace std;

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

int main()
{
	SavingsAccount bank1, bank2;
	int dollars, cents, dol, cen;
	bank1.set(30, 65);//setting bank1 to have initial money as I call upon the void set function above
	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 cent:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.deposit(dol, cen);//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(dol, cen);
	}
	else
	{
		cerr << "Error! Input not valid!" << endl;
		exit(1);
	}
}

void SavingsAccount::deposit(int dol,int cen)
{
	dollars += dol;
	cents += cen;
	if (cents >= 100)
	{
		cents -= dollars;
	}
	
}

void SavingsAccount::withdraw(int dol, int cen)
{
	dollars -= dol;
	cents -= cen;
	if (cents >= 100)
	{
		cents -= dollars;
	}
}

void SavingsAccount::total(int dollars, int cents)
{
	

}



Is This A Good Question/Topic? 0
  • +

Replies To: c++ Savings Account using classes question

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,157
  • Joined: 12-June 08

Re: c++ Savings Account using classes question

Posted 07 September 2019 - 08:59 AM

Explain what the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 Deadweight77   User is offline

  • New D.I.C Head

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

Re: c++ Savings Account using classes question

Posted 07 September 2019 - 09:13 AM

View Postmodi123_1, on 07 September 2019 - 08:59 AM, said:

Explain what the problem is.

I'm trying to set up a savings account in which you can deposit or withdraw money freely while the program keeps track of your total money or balance. It should then display the total money and convert cents to dollars if the cents are greater than or equal to 100. I having trouble with keeping track of the balance and displaying it correctly. I think I'm on the right track in regards to the Deposit and Withdraw function, but I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,157
  • Joined: 12-June 08

Re: c++ Savings Account using classes question

Posted 07 September 2019 - 09:26 AM

You didn't explain the issue. Where are you having problems. What input are you using? What output is it giving? what output should it be giving??
Was This Post Helpful? 0
  • +
  • -

#5 Deadweight77   User is offline

  • New D.I.C Head

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

Re: c++ Savings Account using classes question

Posted 07 September 2019 - 09:39 AM

Well for one I'm having issues with lines 38 and 51 since it says the variables "cen" and "dol" are not initialized. I don't understand why since those lines were fine and working without error before. I know I have other problems, but can't get to them since this problem popped out of nowhere.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,157
  • Joined: 12-June 08

Re: c++ Savings Account using classes question

Posted 07 September 2019 - 09:53 AM

Shouldn't line 51 be the variables from lines 43 and 45? 'dol' is not the same as 'dollars'.
43	        cin >> dollars;
45	        cin >> cents;

51	        bank1.withdraw(dol, cen);

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1