Hello, i need help adding to a class member. I want to make a deposit and add $20 to the class member, however, it doesn't change the value of the class member when i do so.
example: $200 + $20 = $200

And, i assume the other methods won't work either( i didn't code that part yet ).
Any help with the code will be greatly appreciated.

#include <iostream>
#include <string>
#include <conio.h>
#include <Windows.h>

using std::string;

class account
{
private:
	string lname;
	string fname;
	int account_number;
	double savings;
	double checking;

public:
	account(string l, string f, int a, double sav, double chk) : lname(l), fname(f), account_number(a), savings(sav), checking(chk) {}
	void display_balances();
	void withdraw_checking(account w1, double amount);
	void deposit_checking(account d, int n);
	void transfer(double t1, double t2, double amount);
	void buy(double b1, double amount);

	friend void main_menu(account a);
};


void account::display_balances()
{
	std::cout << lname << ", " << fname << std::endl;
	std::cout << "Savings balance: $" << savings << std::endl;
	std::cout << "Checking balance: $" << checking << std::endl;
}

void account::withdraw_checking(account w1, double amount)///////// next
{
	w1.checking -= amount;
	std::cout << "Here is your cash: " << amount << std::endl;
}

void account::deposit_checking(account d, int n)
{
	double a_20 = 20;
	if(n == 1)
		d.checking += a_20; //HERE
	
}

void account::transfer(double t1, double t2, double amount)
{
	t1 -= amount;
	t1 += amount;
}

void account::buy(double b1, double amount)
{
	b1 -= amount;
}


int main()
{
	char yesno;

	account dm("Douglus", "Mark", 1231, 3000.00, 900.00);
	account hm("Heartman", "Mary", 7356, 2310.00, 1020.00);
	account db("Dirtson", "Billy", 8322, 4738.30, 3940.00);

	while(true)
	{
		std::cout << "********************************************************************" << std::endl << std::endl;
		std::cout << "\t\t\tWELCOME TO BANCK OF AMERICA" << std::endl;
		std::cout << "********************************************************************" << std::endl << std::endl;
		
		std::cout << "Please swipe card" << std::endl;
		std::cout << ">> ";
		int user = 2, card;
		std::cin >> card;
		if(!std::cin >> card)
		{
			std::cout << "Wrong input... try again." << std::endl;
			Sleep(3000);
			system("cls");
		}

		if(card == user)
		{
			system("cls");
			
			int n= 3;

			do
			{
				std::cout << "Please enter your pin number" << std::endl;
				std::cout << ">> ";
				int pin;
				std::cin >> pin;

				if(pin == 1231)
				{
					system("cls");
					main_menu(dm);
				}
				else if(pin == 7356)
				{
					system("cls");
					main_menu(hm);
				}
				else if(pin == 8322)
				{
					system("cls");
					main_menu(db);
				}
				else
				{
					std::cout << "Wrong input... you have " << n-- << "tries left before ATM is shut down." << std::endl;
					std::cout << "Press enter to continue." << std::endl;
					_getch();
					system("cls");
				}

			}while( n <= 0);
		}
		else
		{
			std::cout << "Error... wrong input." << std::endl;
			Sleep(4000);
			system("cls");
		}

		std::cout << "Continue? (y/n)" << std::endl;
		std::cout << ">> ";
		std::cin >> yesno;

		if(yesno == 'n' || yesno == 'N')
			break;
		else
			system("cls");
	}

	system("cls");
	std::cout << "Thank you for choosing Bank of America." << std::endl;
	Sleep(4000);

	return 0;
}

void main_menu(account a)
{
	//MAIN MENU
	bool _menu = true; 

	while(_menu)
	{
		std::cout << "\tMAIN MENU" << std::endl << std::endl;
		std::cout << "(1) CHECK BALANCES" << std::endl;
		std::cout << "(2) DEPOSIT MONEY" << std::endl;
		std::cout << "(3) WITHDRAW MONEY" << std::endl;
		std::cout << "(4) TRANSFER TO..." << std::endl;
		std::cout << "(5) QUIT " << std::endl;
		std::cout << ">> ";
		int choice;
		std::cin >> choice;

		switch(choice)
		{
		case 1:

			system("cls");
			a.display_balances();
			std::cout << std::endl;

			std::cout << "Press enter to return to main menu." << std::endl;
			_getch();
			system("cls");
			break;

		case 2:

			while(true)
			{
				system("cls");
				std::cout << "Take from: " << std::endl;
				std::cout << "(1) Checking" << std::endl;
				std::cout << "(2) Savings" << std::endl;
				std::cout << ">> ";
				int pick2;
				std::cin >> pick2;
				if(!std::cin >> pick2)
				{
					std::cout << "Wrong input... try again." << std::endl;
					Sleep(4000);
					system("cls");
				}
				else
				{
					if(pick2 == 1)
					{
						system("cls");
						std::cout << "Pick which amount: " << std::endl;
						std::cout << "(1) $20 " << std::endl;
						std::cout << "(2) $50 " << std::endl;
						std::cout << "(3) $100 " << std::endl;
						std::cout << "(4) $200 " << std::endl;
						std::cout << "(5) $500 " << std::endl;
						std::cout << ">> ";
						int how_much;
						std::cin >> how_much;

						switch(how_much)
						{
						case 1:
							
							a.deposit_checking(a, how_much);
							break; //here
						}
						
						std::cout << std::endl << "Press enter to return to main menu." << std::endl;
						_getch();
						system("cls");
						break;
					}
					else if(pick2 == 2)
					{
						// code next
						

						std::cout << std::endl << "Press enter to return to main menu." << std::endl;
						_getch();
						system("cls");
						break;
					}
					else
					{
						std::cout << "Wrong input... try again." << std::endl;
						Sleep(4000);
					}
					
				}
			}

		case 3:
			// code later
			break;

		case 4:
			//code later
			break;

		case 5:
			//quit
			_menu = false;
			break;

		default:

			std::cout << "Error has occured... please try again." << std::endl;
			Sleep(4000);
			system("cls");
			break;
		}
	}
}

Recommended Answers

All 4 Replies

You can fix your class so that you don't pass it back to itself to make transactions.

Remove the class parameter of the deposit method and have the class modify its own balance directly.

As thines01 says, a better option is:

void account::deposit_checking(int n)
{
	double a_20 = 20;
	if(n == 1)
		checking += a_20;
 
}

The rest of my post explains why your original version didn't work.

void account::deposit_checking(account d, int n)
{
	double a_20 = 20;
	if(n == 1)
		d.checking += a_20; //HERE
 
}

This code receives a copy of an account. This is known as "pass by value". You are altering that copy, and when the function is finished, the original has not been changed. All changes happen to the copy, and the copy is destroyed when the function finishes.

If you want to change the original account, pass by reference.

void account::deposit_checking(account[B]&[/B] d, int n)
{
	double a_20 = 20;
	if(n == 1)
		d.checking += a_20;
 
}

http://www.cplusplus.com/doc/tutorial/functions2/

You'll have to insert the & in the prototype as well.

void deposit_checking(account& d, int n);

Thanks for the help thines01 and Moschops! I need all the help I can get. My object oriented teacher isn't very good... even though he says he graduated from MIT.

Do, did you fix it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.