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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

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

 
0
  #1
Jun 12th, 2008
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
  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]





  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]





  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,437
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

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

 
0
  #2
Jun 12th, 2008
Please Use code tags, It makes reading it so much easier
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

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

 
0
  #3
Jun 12th, 2008
Sorry about that!! hope this is right

//Lab 2 Account.h
//Definition of the Account class
  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.
  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; 
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #4
Jun 12th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC