943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 502
  • C++ RSS
Jun 12th, 2008
0

Need help with an exercise program (probably too easy for you guys)

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. //Definition of the Account class
  2.  
  3. [class Account
  4. {
  5. public:
  6. Account( int ); // constructor initializes balance
  7. void credit( int ); // add an amount to the account balance
  8. void debit( int ); // subtract an amount to the account balance
  9. int getBalance(); // return the account balance
  10. private:
  11. int balance; // data member that stores the balance
  12. }; // end class Account]





C++ Syntax (Toggle Plain Text)
  1. // Lab 2: Account.cpp
  2. // Member-function definition for class Account.
  3. #include <iostream>
  4. using std::cout;
  5. using std::endl;
  6.  
  7. #include "Account.h" // include the definition of class Account
  8.  
  9. // Account constructor initializes data member balance
  10. Account::Account ( int initialBalance )
  11. {
  12. balance = 0; // assume that the balance begins at 0
  13.  
  14. // if initial value is greater than 0, set this value as the
  15. // balance of the Account; otherwise, balance remains 0
  16. if ( initialBalance > 0 )
  17. balance = initialBalance;
  18.  
  19. // if initial balance is negative, print error message
  20. if ( initialBalance < 0 )
  21. cout << "Error: Initial balance cannot be negative.\n" << endl;
  22. } // end Account constructor
  23.  
  24. // credit (add) an amount to the account balance
  25. void Account::credit(int amount)
  26. {
  27. balance = balance + amount; // add amount to balance
  28. } // end function credit
  29.  
  30. // debit (subtract) an amount to the account balance
  31. void Account::debit(int amount)
  32. {
  33. if ( amount > balance ) // if the withdrawal amount is more than
  34. // the current balance
  35. cout << "Debit \"" << amount << "\" exceeded account balance. \n";
  36. if ( amount <= balance )
  37. balance = balance - amount;
  38.  
  39. } // end function debit
  40.  
  41. // return the account balance
  42. int Account::getBalance()
  43. {
  44. return balance; // gives the value of balance to the calling function
  45. } // end function getBalance]





C++ Syntax (Toggle Plain Text)
  1. // Lab 2: AccountTest.cpp
  2. // Create and manipulate Account objects
  3. #include <iostream>
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. // include definition of class Account from Account.h
  9. #include "Account.h"
  10.  
  11. // function main begins program execution
  12. int main()
  13. {
  14. Account account1( 50 ); // create Account object
  15. Account account2( 0 ); //create Account object
  16.  
  17. // display initial balance of each object
  18. cout << "account1 balance: $" << account1.getBalance() << endl;
  19. cout << "account2 balance: $" << account2.getBalance() << endl;
  20.  
  21. int withdrawalAmount; // stores withdrawal amount stored from user
  22.  
  23. cout << "\nEnter withdrawal amount for account1: "; // prompt
  24. cin >> withdrawalAmount; // obtain user input
  25. cout << "\nsubtracting " << withdrawalAmount
  26. << " from account1 balance\n\n";
  27. /* write code to withdrawal money from account1 */
  28.  
  29. //display balances
  30. cout << "account1 balance: $" << account1.getBalance() << endl;
  31. cout << "account2 balance: $" << account2.getBalance() << endl;
  32.  
  33. cout << "\nEnter withdrawal amount for account2: "; // prompt
  34. cin >> withdrawalAmount; // obtain user input
  35. cout << "\nsubtracting " << withdrawalAmount
  36. << " from account2 balance\n\n";
  37. /* write code to withdrawal money from account2 */
  38.  
  39. //display balances
  40. cout << "account1 balance: $" << account1.getBalance() << endl;
  41. cout << "account2 balance: $" << account2.getBalance() << endl;
  42. return 0; // indicate successful termination
  43. } // end main
Last edited by Ancient Dragon; Jun 12th, 2008 at 8:08 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Jun 12th, 2008
0

Re: Need help with an exercise program (probably too easy for you guys)

Please Use code tags, It makes reading it so much easier
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 12th, 2008
0

Re: Need help with an exercise program (probably too easy for you guys)

Sorry about that!! hope this is right

//Lab 2 Account.h
//Definition of the Account class
C++ Syntax (Toggle Plain Text)
  1. class Account
  2. {
  3. public:
  4. Account( int );
  5. void credit( int );
  6. void debit( int );
  7. int getBalance();
  8. private:
  9. int balance;
  10. };


// Lab 2: Account.cpp
// Member-function definition for class Account.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. #include "Account.h"
  6.  
  7. Account::Account ( int initialBalance )
  8. {
  9. balance = 0;
  10.  
  11. if ( initialBalance > 0 )
  12. balance = initialBalance;
  13.  
  14. if ( initialBalance < 0 )
  15. cout << "Error: Initial balance cannot be negative.\n" << endl;
  16. }
  17.  
  18. void Account::credit(int amount)
  19. {
  20. balance = balance + amount;
  21. }
  22.  
  23. void Account::debit(int amount)
  24. {
  25. if ( amount > balance )
  26. cout << "Debit \"" << amount << "\" exceeded account balance. \n";
  27. if ( amount <= balance )
  28. balance = balance - amount;
  29. }
  30.  
  31. int Account::getBalance()
  32. {
  33. return balance;
  34. }


// 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; 
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Jun 12th, 2008
0

Re: Need help with an exercise program (probably too easy for you guys)

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]
Last edited by Ancient Dragon; Jun 12th, 2008 at 8:18 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error: initial value of reference to non-const must be an lvalue
Next Thread in C++ Forum Timeline: I need help with a save function in my game.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC