I need help where it say /* write code to withdrawal money from account1 */.. i think the member function is written right but im not sure.
Im a true begginer taking this online course and I think i have most of it right, can someone please help guide me.. here what i got

[//Lab 2 Account.h

//Definition of the Account class

[class Account
{
public:
Account( int ); // constructor initializes balance
void credit( int ); // add an amount to the account balance
void debit( int ); // subtract an amount to the account balance
int getBalance(); // return the account balance
private:
int balance; // data member that stores the balance
}; // end class Account]
// Lab 2: Account.cpp
// Member-function definition for class Account.
#include <iostream>
using std::cout;
using std::endl;

#include "Account.h" // include the definition of class Account

// Account constructor initializes data member balance
Account::Account ( int initialBalance )
{
balance = 0; // assume that the balance begins at 0

// if initial value is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0
if ( initialBalance > 0 )
balance = initialBalance;

// if initial balance is negative, print error message
if ( initialBalance < 0 )
cout << "Error: Initial balance cannot be negative.\n" << endl;
} // end Account constructor

// credit (add) an amount to the account balance
void Account::credit(int amount)
{
balance = balance + amount; // add amount to balance
} // end function credit

// debit (subtract) an amount to the account balance
void Account::debit(int amount)
{
if ( amount > balance ) // if the withdrawal amount is more than
// the current balance
cout << "Debit \"" << amount << "\" exceeded account balance. \n";
if ( amount <= balance )
balance = balance - amount;

} // end function debit

// return the account balance
int Account::getBalance()
{
return balance; // gives the value of balance to the calling function
} // end function getBalance]
// Lab 2: AccountTest.cpp
// Create and manipulate Account objects
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

// include definition of class Account from Account.h
#include "Account.h"

// function main begins program execution
int main()
{
Account account1( 50 ); // create Account object
Account account2( 0 ); //create Account object

// display initial balance of each object
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;

int withdrawalAmount; // stores withdrawal amount stored from user

cout << "\nEnter withdrawal amount for account1: "; // prompt
cin >> withdrawalAmount; // obtain user input
cout << "\nsubtracting " << withdrawalAmount
<< " from account1 balance\n\n";
/* write code to withdrawal money from account1 */

//display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;

cout << "\nEnter withdrawal amount for account2: "; // prompt
cin >> withdrawalAmount; // obtain user input
cout << "\nsubtracting " << withdrawalAmount
<< " from account2 balance\n\n";
/* write code to withdrawal money from account2 */

//display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
return 0; // indicate successful termination
} // end main

Recommended Answers

All 3 Replies

Please Use code tags, It makes reading it so much easier :)

Sorry about that!! hope this is right

//Lab 2 Account.h
//Definition of the Account class

class Account
{
public:
Account( int ); 
void credit( int ); 
void debit( int ); 
int getBalance(); 
private:
int balance;
};

// Lab 2: Account.cpp
// Member-function definition for class Account.

#include <iostream>
using std::cout;
using std::endl;

#include "Account.h" 

Account::Account ( int initialBalance )
{
balance = 0; 

if ( initialBalance > 0 )
balance = initialBalance;

if ( initialBalance < 0 )
cout << "Error: Initial balance cannot be negative.\n" << endl;
} 

void Account::credit(int amount)
{
balance = balance + amount; 
} 

void Account::debit(int amount)
{
if ( amount > balance ) 
cout << "Debit \"" << amount << "\" exceeded account balance. \n";
if ( amount <= balance )
balance = balance - amount;
} 

int Account::getBalance()
{
return balance; 
}

// Lab 2: AccountTest.cpp
// Create and manipulate Account objects

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "Account.h"

int main()
{
Account account1( 50 );
Account account2( 0 ); 

cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;

int withdrawalAmount; 

cout << "\nEnter withdrawal amount for account1: ";
cin >> withdrawalAmount; 
cout << "\nsubtracting " << withdrawalAmount
<< " from account1 balance\n\n";
/* write code to withdrawal money from account1 */

cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;

cout << "\nEnter withdrawal amount for account2: "; 
cin >> withdrawalAmount; 
cout << "\nsubtracting " << withdrawalAmount
<< " from account2 balance\n\n";
/* write code to withdrawal money from account2 */


cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
return 0; 
}

To withdraw money you will need to add another method to the Account class

//Definition of the Account class

[class Account
{
public:
    Account( int ); // constructor initializes balance
    void credit( int ); // add an amount to the account balance
    void debit( int ); // subtract an amount to the account balance
    int getBalance(); // return the account balance

    void withdraw(int amount);

private:
     int balance; // data member that stores the balance
}; // end class Account]
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.