Like Tree1Likes
  • 1 Post By laserlight

need help with a program

This is a discussion on need help with a program within the C++ Programming forums, part of the General Programming Boards category; create c++ programm that defines a class to represent a bank account. Data members should include the depositor’s name, the ...

  1. #1
    K13
    K13 is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    need help with a program

    create c++ programm that defines a class to represent a bank account.
    Data members should include the depositor’s name, the account number(use a string), and the balance.
    Member functions should allow the following:
    1)Creating an object and initializing it.
    2)Displaying the depositor’s name, account number, and balance.
    3)Depositing an amount of money given by an argument.
    4)Withdrawing an amount of money given by an argument.

    having trouble with the member funtion ShowInfo() which has to output DepositorName, AccountNumber, and Balance


    Code:
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    class BankAccount
    {
    private:
            std::string DepositorName;
            std::string AccountNumber;
            double Balance;
    public:
            BankAccount();
            BankAccount(std::string depname, std::string acnum, double bal );
            ~BankAccount();  //destructor
            void ShowInfo() const;
            void Deposit(double depamount);
            void Withdraw(double amount);
    };
    
    //non-defualt constructor
    BankAccount::BankAccount(std::string depname, std::string acnum,  double bal)
    {
               DepositorName = depname;
               AccountNumber = acnum;
               Balance = bal;
    }
    
    //destructor
    BankAccount::~BankAccount()
    {
    }
    
    void BankAccount::Withdraw(double amount)
    {
            if (amount < 0)
                  std::cout << "\nPlease enter positive number for the amount to withdraw.";
            else
                    Balance -= amount;
    }
    
    
    void BankAccount::Deposit(double depamount)
    {
              if (depamount < 0)
                   std::cout << "\nPlease enter positive number for  the amount to deposit.";
              else
                      Balance+=depamount;
    }
    
    
    void BankAccount::ShowInfo() const
    {
    	
    	cout << DepositorName;
    	cout << AccountNumber;
        cout << Balance;
    }
    
    
    
    int main()
    {
    	BankAccount somename("some name", "08973541", 60000);
    
    	somename.ShowInfo();
    
    	somename.Deposit(15000);
    
    	somename.ShowInfo();
    
    	somename.Withdraw(30000);
    
    	somename.ShowInfo();
    }
    Last edited by K13; 08-08-2011 at 10:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    How does it not work? One thing I notice is that you use std::string but did not #include <string>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by K13
    it works, now. wow can i delete this post
    Please do not do edit out your answer. You basically made my reply look stupid, and when I look stupid, I may decide not to answer your questions and close your thread so as to deny you any answers. In other words, out of courtesy to other members of this community, leave your post as-is so that the thread still makes sense to a future reader.
    AndrewHunter likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    It is extreme bad form to edit your posts after you solved the problem. The main reason for these forums is for people to learn from eachother. No one here is paid for contributing their time; removing your posts negates the work done by the people here who contribute there time to assist because it prevents others from learning. If you do not want to contribute to the community here then find your way to another online resource, such as a paid one.

    EDIT: Too late.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    K13
    K13 is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    sorry will post the code up as was in a sec

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by laserlight View Post
    How does it not work? One thing I notice is that you use std::string but did not #include .
    Is it necessary ?I just found out that code using std::string works (in gcc-4.6) without including string . Does the linker automatically find out the classes declared in the standard library but not the objects ?(std::cout always needs iostream) ?
    Last edited by manasij7479; 08-09-2011 at 12:08 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by manasij7479
    Is it necessary ?I just found out that code using std::string works (in gcc-4.6) without including string .
    You may have included some other header that includes <string>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Does iostream and/or fstream include string ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by manasij7479
    Does iostream and/or fstream include string ?
    It depends on the implementation.

    To give you a straight answer: simply put, you need to #include <string>, whether directly or indirectly, to use std::string objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  4. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21